本帖最后由 sinanjj 于 2010-11-26 09:47 编辑
http://www.tkdocs.com/tutorial/firstexample.html
一个真实的例子
画个草图:
我们有一个按钮, 一个输入框, 3个lable
分割如下:
代码:
from tkinter import *
from tkinter import ttk
def calculate(*args):
try:
value = float(feet.get())
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
root = Tk()
root.title("Feet to Meters")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
feet = StringVar()
meters = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()
一步一步的解释下
from tkinter import *
from tkinter import ttk
导入库.
root = Tk()
root.title("Feet to Meters")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
先跳开calculate函数, 因为那还没被调用.
以上的这些代码设置主窗口, 设置标题为 "Feet to Meters". 然后我们创建一个frame控件, 并把它放到主窗口. "columnconfigure"/"rowconfigure" 告诉主窗口如果从拉伸就要自己从新设置.
feet = StringVar()
meters = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)
上边的代码创建了3个控件: 一个输入框,一个lable, 一个按钮.
ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
创建并放置3个label
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', calculate)
The preceeding three lines help put some nice finishing touches on our user interface.
The first line walks through all of the widgets that are children of our content frame,and adds a little bit of padding around each, so they aren't so scrunched together.We could have added these options to each "grid" call when we first put the widgetsonscreen, but this is a nice shortcut.The second line tells Tk to put the focus on our entry widget. That way the cursor willstart in that field, so the user doesn't have to click in it before starting to type.
The third line tells Tk that if the user presses the Return key (Enter on Windows) anywherewithin the root window, that it should call our calculate routine, the same as if theuser pressed the Calculate button.
def calculate(*args):
try:
value = float(feet.get())
meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
Here we define our calculate procedure, which is called either when the user pressesthe Calculate button, or hits the Return key. It performs the feet to meterscalculation, taking the number of feet from our entry widget, and placing the resultin our label widget.
Say what? It doesn't look like we're doing anything with those widgets! Here's wherethe magic "textvariable" options we specified when creating the widgets come into play. We specified the global variable "feet" as the textvariable for the entry, which meansthat anytime the entry changes, Tk will automatically update the global variable feet.Similarly, if we explicitly change the value of a textvariable associated with a widget(as we're doing for "meters" which is attached to our label), the widget will automaticallybe updated with the current contents of the variable. Slick.
root.mainloop();This final line tells Tk to enter its event loop, which is needed to make everything run. |