python Tkinter例子

Python可用的GUI编程的包很多,Tkinter也是其中一个半标准的工具包。 作为一个老牌的Python GUI工具包(皮皮书屋里找了本书,竟然是2001年的),它由Tk GUI包装而来。在Windows版里面已经包括了,不用单独下载。

tkinter简介:
# tkinter 是python的一个模块,封装了Tcl/Tk接口,
# Tcl/Tk是跨平台脚本图形界面接口。
# tk的底层C语言接口在动态链接库_tkinter中。
 
# 注意,在2.*版本中 使用的方法是
# from Tkinter import *
# 或者
# import Tkinter
 
# 但是在最新的3.*中则是小写,即
from tkinter import *
# 或者
import tkinter
# 否则会报找不到模块错误



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

# -*- coding: utf-8 -*-

__author__ = 'xsc' 

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() 	#进入主循环,程序运行
运行显示结果如下:




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

# -*- coding: utf-8 -*-

__author__ = 'xsc' 

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()




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

# -*- coding: utf-8 -*-

__author__ = 'xsc'

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()



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


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

# -*- coding: utf-8 -*-
__author__ = 'xsc' 

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)  	

#创建另一个下拉菜单Edit 
editmenu = 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) 	

#创建下拉菜单Help 
helpmenu = Menu(menubar, tearoff=0) 
helpmenu.add_command(label="About", command=about) 
menubar.add_cascade(label="Help", menu=helpmenu)  	

#显示菜单 
root.config(menu=menubar)  
mainloop()



  写了这一些,差不多对Tkinter有了一个大体的印象了。在Python中用Tkinter绘制GUI界面还是蛮简单的。再把上面的例子扩展一下,和Label标签结合,当单击about的时候,在窗体上打印About的内容,而不是在终端输出。将about函数稍微修改一下。单击about以后将会调用about函数渲染frame绘制一个标签并显示其内容。


  关于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上应该可以找的到








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值