简单介绍如何使用Tkinter进行GUI编程。

from tkinter import *
import tkinter.messagebox as messagebox
'''在GUI中,每个Button、Label、输入框等,都是一个Widget。
Frame则是可以容纳其他Widget的Widget,所有的Widget组合起来就是一棵树。
pack()方法把Widget加入到父容器中,并实现布局。
pack()是最简单的布局,grid()可以实现更复杂的布局。
在createWidgets()方法中,我们创建一个Label,两个Button,一个文本框。当Quit被点击时,触发self.quit()使程序退出,
当hello被点击时,触发self.hello(),显示message界面'''
class Application(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.pack()
        self.createWidgets()
    def createWidgets(self):
        self.nameInput=Entry(self)
        self.nameInput.pack()
        self.helloLabel=Label(self,text='hello world')
        self.helloLabel.pack()
        self.helloButton=Button(self,text='hello',command=self.hello)
        self.helloButton.pack()
        self.quitButton=Button(self,text='Quit',command=self.quit)
        self.quitButton.pack()
    def hello(self):
        name=self.nameInput.get() or 'world'
        messagebox.showinfo('Message','Hello %s'%name)
if __name__ == '__main__':
    app=Application()
    '''设置窗口标题'''
    app.master.title='liuruiqing'
    '''主程序循环'''
    app.mainloop()

 

posted on 2017-07-29 18:31  大瑞清创世录  阅读(202)  评论(0)    收藏  举报