参考,翻译,转述 自:http://www.pythonware.com/library/tkinter/introduction/index.htm
Tkinter是一种python的编程接口,基于tk库,而tk是一组GUI toolkits,解决了跨平台UI显示。
简单说,也就是我们可以使用Tkinter和Python来编写图形应用程序员。使用Tkinter十分的简单,只要在Python中导入该模块就可以使
用了。
【Example 1】下面是最小化的hello world的一个示例:
1 # File: hello1.py 2 3 from Tkinter import * #导入模块 4 5 root = Tk() #实例化根节点 6 7 w = Label(root, text="Hello, world!") #实例化一个子节点label 8 w.pack() #对label进行大小和可见性的相关设置 9 10 root.mainloop() #启动应用程序事件循环
运行,显示一个标签的窗口,窗口标题为Tk.
【Example 2】下面,使用类来对子控件的创建进行封装:
# File: hello2.py
from Tkinter import *
# 将子控件打包在App类,并绑定到相同的成员方法
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root) #实例化时传入root节点
root.mainloop() #同样启动事件循环