初学Python GUI 编程,Tkinter入门
窗口和控件
在GUI编程中,顶层根窗口包含组成GUI应用的所有小窗口对象,可以是标签,按钮,列表框等等。
Python中生成顶层窗口:
// An highlighted block
import tkinter
top = tkinter.Tk()
## Lable控件
显示tkinter版本的hello world:
import tkinter
top = tkinter.Tk()
label = tkinter.Label(top, text='hello world!')
label.pack() ##用packer来管理和显示控件
tkinter.mainloop() ##运行GUI程序
## Button控件
import tkinter
top = tkinter.Tk()
quit = tkinter.Button(top,text='QUIT', bg='red', fg='white') ## 设置退出按钮,并且设置按钮背景色和字体色
quit.pack()
tkinter.mainloop()
## 两个程序组合在一起
import tkinter
top = tkinter.Tk()
label = tkinter.Label(top, text='hello world!')
label.pack() ##用packer来管理和显示控件
quit = tkinter.Button(top,text='QUIT', bg='red', fg='white') ## 设置退出按钮,并且设置按钮背景色和字体色
quit.pack()
tkinter.mainloop() ##运行GUI程序
运行结果: