码迷,mamicode.com
首页 > 其他好文 > 详细

Tkinter学习笔记-2

时间:2014-12-14 00:31:35      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   on   div   log   

事件处理模型

GUI组件根据用户的交互情况生成事件

异步事件驱动的程序:程序将事件绑定到图形组件上,并实现了事件的处理程序(回调函数)来处理GUI事件

 

文本框

用于输入文本,程序员用来显示文本

通过Entry类创建

用户按下Enter键触发Entry组件的<Return>事件

 1 # SimpleEntry.py
 2 # Entry compnents and event binding demonstration
 3 
 4 from Tkinter import *
 5 from tkMessageBox import *
 6 
 7 class EntryDemo( Frame ):
 8     """ Demonstrate Entrys and Event binding """
 9 
10     def __init__(self):
11         """ Create, pack and bind events to four Entrys """
12 
13         Frame.__init__( self )
14         self.pack( expand = YES, fill = BOTH )
15         self.master.title( "Testing Entry Components" )
16         self.master.geometry( "325x100" ) # width x length
17 
18         self.frame1 = Frame( self )
19         self.frame1.pack( pady = 5 )
20 
21         self.text1 = Entry( self.frame1, name = "text1" )
22 
23         # bind the Entry component to event
24         self.text1.bind( "<Return>", self.showContents )
25         self.text1.pack( side = LEFT, padx = 5 )
26 
27         self.text2 = Entry( self.frame1, name = "text2" )
28 
29         # insert text into Entry component text2
30         self.text2.insert( INSERT, "Enter text here" )
31         self.text2.bind("<Return>", self.showContents )
32         self.text2.pack( side = LEFT, padx = 5 )
33 
34         self.frame2 = Frame( self )
35         self.frame2.pack( pady = 5 )
36 
37         self.text3 = Entry( self.frame2, name = "text3" )
38         self.text3.insert( INSERT, "Uneditable text field" )
39 
40         # prohibit user from altering text in Entry component text3
41         self.text3.config( state = DISABLED )
42         self.text3.bind("<Return>", self.showContents )
43         self.text3.pack( side = LEFT, padx = 5 )
44 
45         # text in Entry component text4 appears as *
46         self.text4 = Entry(self.frame2, name = "text4", show = "*" )
47         self.text4.bind("<Return>", self.showContents )
48         self.text4.pack( side = LEFT, padx = 5 )
49 
50     def showContents( self, event ):
51         """ Display the contents of the Entry """
52 
53         # acquire name of Entry component that genereated event
54         theName = event.widget.winfo_name()
55 
56         # acquire contents of Entry component that generated event
57         theContents = event.widget.get()
58         showinfo( "Message", theName + ":" + theContents)
59 
60 
61 demo = EntryDemo()
62 
63 demo.mainloop()

 

Tkinter学习笔记-2

标签:style   blog   io   ar   color   sp   on   div   log   

原文地址:http://www.cnblogs.com/tmmuyb/p/4162097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!