博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tkinter学习笔记-2
阅读量:5218 次
发布时间:2019-06-14

本文共 2437 字,大约阅读时间需要 8 分钟。

事件处理模型

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 length17 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 event24         self.text1.bind( "
", 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 text230 self.text2.insert( INSERT, "Enter text here" )31 self.text2.bind("
", 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 text341 self.text3.config( state = DISABLED )42 self.text3.bind("
", 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("
", 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 event54 theName = event.widget.winfo_name()55 56 # acquire contents of Entry component that generated event57 theContents = event.widget.get()58 showinfo( "Message", theName + ":" + theContents)59 60 61 demo = EntryDemo()62 63 demo.mainloop()

 

转载于:https://www.cnblogs.com/tmmuyb/p/4162097.html

你可能感兴趣的文章
Java使用FileReader(file)、readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号。...
查看>>
Elipse安装Spring Tool Suite
查看>>
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>
log4j.properties的作用
查看>>
滴滴快车历史奖励政策:含工作日和周末的高峰奖励、翻倍奖励【历史政策】...
查看>>
文件操作类2
查看>>
游戏偶感
查看>>
Leetcode: Unique Binary Search Trees II
查看>>
phpcurl类
查看>>
C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
查看>>
Spring-hibernate整合
查看>>
cocos2dx 3.3环境搭建
查看>>
Spring DI
查看>>
c++ map
查看>>
exit和return的区别
查看>>
ThinkPHP5.1安装
查看>>
在Mac OS X中搭建STM32开发环境(2)
查看>>
vi 常用命令
查看>>
js += 含义(小知识)
查看>>