19.3 Tkinter举例
19.3.1 标签组件
import Tkinter
top = Tkinter.Tk()
label = Tkinter.Label(top, text = 'Hello World!')
label.pack()
Tkinter.mainloop()
label.pack()指明用packer来管理和显示组件。
19.3.2 按钮组件import Tkinter
top = Tkinter.Tk()
quit = Tkinter.Button(top, text = 'Hello World!', command = top.quit)
quit.pack()
Tkinter.mainloop()
按钮回调top.quit,让整个程序退出。
19.3.3 标签和按钮组件
import Tkinter
top = Tkinter.Tk()
hello = Tkinter.Label(top, text = 'Hello World!')
hello.pack()
quit = Tkinter.Button(top, text = 'QUIT', command = top.quit, bg = 'red', fg = 'white')
quit.pack(fill = Tkinter.X, expand = 1)
Tkinter.mainloop()
19.3.4 标签、按钮和进度条组件
from Tkinter import *
def resize(ev = None):
label.config(font = 'Helvetica -%d bold' % scale.get())
top = Tk()
hello = Label(top, text = 'Hello World!', font = 'Helvetica -12 bold')
hello.pack(fill = Y, expand = 1)
scale = Scale(top, from_ = 10, to = 40, orient = HORIZONTAL, command = resize)
scale.set(12)
scale.pack(fill = X, expand = 1)
quit = Button(top, text = 'QUIT', command = top.quit, bg = 'red', fg = 'white')
quit.pack()
mainloop()