Python下用Tkinter进行GUI编程

来源:互联网 发布:java电信项目 编辑:程序博客网 时间:2024/05/16 17:42

Python下用Tkinter进行GUI编程

    Python可用的GUI编程的包很多,Tkinter也是其中一个半标准的工具包。

    作为一个老牌的Python GUI工具包(皮皮书屋里找了本书,竟然是2001年的),它由Tk GUI包装而来。在Windows版里面已经包括了,不用单独下载。

    用Tkinter实现一个简单的GUI程序,单击click按钮时会在终端打印出’hello world’:

复制代码
__author__ = 'fyby'from Tkinter import *   #引入Tkinter工具包def hello():    print('hello world!')win = Tk()  #定义一个窗体win.title('Hello World')    #定义窗体标题win.geometry('400x200')     #定义窗体的大小,是400X200像素btn = Button(win, text='Click me', command=hello)#注意这个地方,不要写成hello(),如果是hello()的话,#会在mainloop中调用hello函数,# 而不是单击button按钮时出发事件btn.pack(expand=YES, fill=BOTH) #将按钮pack,充满整个窗体(只有pack的组件实例才能显示)mainloop() #进入主循环,程序运行
复制代码

 

image

 

    当我们写一个较大的程序时,最好将代码分成一个或者是几个类,再看一下Hello World例子

复制代码
#-*- encoding=UTF-8 -*-__author__ = 'fyby'from Tkinter import *class App:    def __init__(self, master):        #构造函数里传入一个父组件(master),创建一个Frame组件并显示        frame = Frame(master)        frame.pack()        #创建两个button,并作为frame的一部分        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)        self.button.pack(side=LEFT) #此处side为LEFT表示将其放置 到frame剩余空间的最左方        self.hi_there = Button(frame, text="Hello", command=self.say_hi)        self.hi_there.pack(side=LEFT)    def say_hi(self):        print "hi there, this is a class example!"win = Tk()app = App(win)win.mainloop()
复制代码

 

image

    看完了上面两个无聊的Hello World例子,再来看一个稍微Perfect点的东西吧。Menu组件,自己画一个像样点的程序外壳。

复制代码
#-*- encoding=UTF-8 -*-__author__ = 'fyby'from Tkinter import *root = Tk()def hello():    print('hello')# 创建一个导航菜单menubar = Menu(root)menubar.add_command(label="Hello!", command=hello)menubar.add_command(label="Quit!",command=root.quit)root.config(menu=menubar)mainloop()
复制代码

 

image

        这个程序还是有点无趣,因为我们只是创建了一个顶级的导航菜单,点击后只是在终端中输出hello而已,下面来创建一个下拉菜单,这样才像一个正儿八经的应用大笑在下面的这个例子中,会创建三个顶级菜单,每个顶级菜单中都有下拉菜单(用add_command方法创建,最后用add_cascade方法加入到上级菜单中去),为每个下拉选项都绑定一个hello函数,在终端中打印出hello.

        root.quit是退出这个Tk的实例。

复制代码
#-*- encoding=UTF-8 -*-__author__ = 'fyby'from Tkinter import *root = Tk()def hello():    print('hello')def about():    print('我是开发者')menubar = Menu(root)#创建下拉菜单File,然后将其加入到顶级的菜单栏中filemenu = Menu(menubar,tearoff=0)filemenu.add_command(label="Open", command=hello)filemenu.add_command(label="Save", command=hello)filemenu.add_separator()filemenu.add_command(label="Exit", command=root.quit)menubar.add_cascade(label="File", menu=filemenu)#创建另一个下拉菜单Editeditmenu = Menu(menubar, tearoff=0)editmenu.add_command(label="Cut", command=hello)editmenu.add_command(label="Copy", command=hello)editmenu.add_command(label="Paste", command=hello)menubar.add_cascade(label="Edit",menu=editmenu)#创建下拉菜单Helphelpmenu = Menu(menubar, tearoff=0)helpmenu.add_command(label="About", command=about)menubar.add_cascade(label="Help", menu=helpmenu)#显示菜单root.config(menu=menubar)mainloop()
复制代码

 

 

image

  写了这一些,差不多对Tkinter有了一个大体的印象了。在Python中用Tkinter绘制GUI界面还是蛮简单的。再把上面的例子扩展一下,和Label标签结合,当单击about的时候,在窗体上打印About的内容,而不是在终端输出。将about函数稍微修改一下。单击about以后将会调用about函数渲染frame绘制一个标签并显示其内容。
def about():    w = Label(root,text="开发者感谢名单\nfuyunbiyi\nfyby尚未出现的女朋友\nhttp://www.programup.com网站")    w.pack(side=TOP)

 

image

 

        关于Tkinter更多的内容,参考http://www.programup.com/wiki/beginning_tkinter/,例子原型主要来自于该网站,还有一本书(就是文章开头提到过的那那本01年的书,http://www.ppurl.com/2012/02/python%E4%B8%8Etkinter%E7%BC%96%E7%A8%8B.html)。还有,国内的书记得《征服Python》中好像也有关于Tkinter的例子,在VeryCD上应该可以找的到大声笑

【转载】:http://www.cnblogs.com/fuyunbiyi/archive/2012/06/13/2548497.html


0 0