让 Packer 来管理和显示控件,最后调用 mainloop() 运行 GUI 应用。
>>> import tkinter
>>> top = tkinter.Tk()
>>> label = tkinter.Label(top, text="数据云团")
>>> label.pack()
>>> tkinter.mainloop()
创建按钮控件
import tkinter
top = tkinter.Tk()
quit = tkinter.Button(top, text='退出', command=top.quit)
quit.pack()
tkinter.mainloop()
该按钮有一个额外的参数,tkinter.quit() 方法。该参数会给按钮安装一个回调函数,当按钮被按下(并释放后),整个程序就会退出。
fill 参数告诉 Packer 让 QUIT 按钮占据剩余的水平空间,而 expand 参数则会引导它填充整个水平可视空间,将按钮拉伸到左右窗口边缘。
import tkinter
top = tkinter.Tk()
text = tkinter.Label(top, text="数据云团")
text.pack()
quit = tkinter.Button(top, text="加鸡腿", command=top.quit, bg="red", fg="white")
quit.pack(fill=tkinter.X, expand=1)
tkinter.mainloop()
Scale 滑块是用来控制 Label 控件中文字字体大小的工具。滑块的位置值越大,字体越大。
Scale 用于与 Label 控件进行交互。
import tkinter
def resize(ev=None):
label.config(font="Helvetica -%d bold" % scale.get())
top = tkinter.Tk()
top.geometry('250x150')
label = tkinter.Label(top, text="数据云团", font="Helvetica -12 bold")
label.pack(fill=tkinter.Y, expand=1)
scale = tkinter.Scale(top, from_=10, to=40, orient=tkinter.HORIZONTAL, command=resize)
scale.set(12)
scale.pack(fill=tkinter.X, expand=1)
quit = tkinter.Button(top, text="点击", command=top.quit, activeforeground="white", activebackground="red")
quit.pack()
tkinter.mainloop()