设为首页 收藏本站
查看: 1988|回复: 0

[经验分享] python开发_tkinter_获取文本框内容_给文本框添加键盘输入事件

[复制链接]

尚未签到

发表于 2015-4-25 11:14:53 | 显示全部楼层 |阅读模式
  在之前的blog中有提到python的tkinter中的菜单操作
  python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐
  python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
  python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐
  python开发_tkinter_复选菜单
  python开发_tkinter_单选菜单_不可用菜单操作
  python开发_tkinter_多级子菜单
  python开发_tkinter_获取单选菜单值
  下面是tkinter的获取文本框内容_给文本框添加键盘输入事件的操作
  运行效果:
  在输入的时候,单击回车键,触发:print_contents()
DSC0000.png
  ==========================================================
  代码部分:
  ==========================================================



1 from tkinter import *
2
3 __author__ = {'name' : 'Hongten',
4               'mail' : 'hongtenzone@foxmail.com',
5               'blog' : 'http://www.iyunv.com/',
6               'QQ': '648719819',
7               'created' : '2013-09-11'}
8
9 # This program  shows how to make a typein box shadow a program variable.
10 flag = True
11 class App(Frame):
12     def __init__(self, master=None):
13         Frame.__init__(self, master)
14         self.pack()
15
16         self.entrythingy = Entry(self)
17         self.entrythingy.pack()
18
19         self.button = Button(self, text="Uppercase The Entry",
20                              command=self.upper)
21         self.button.pack()
22
23         # here we have the text in the entry widget tied to a variable.
24         # changes in the variable are echoed in the widget and vice versa.
25         # Very handy.
26         # there are other Variable types. See Tkinter.py for all
27         # the other variable types that can be shadowed
28         self.contents = StringVar()
29         self.contents.set("this is a variable")
30         self.entrythingy.config(textvariable=self.contents)
31
32         # and here we get a callback when the user hits return. we could
33         # make the key that triggers the callback anything we wanted to.
34         # other typical options might be  or  (for anything)
35         self.entrythingy.bind('', self.print_contents)
36
37     def upper(self):
38         # notice here, we don't actually refer to the entry box.
39         # we just operate on the string variable and we
40         # because it's being looked at by the entry widget, changing
41         # the variable changes the entry widget display automatically.
42         # the strange get/set operators are clunky, true...
43         global flag
44         flag = not flag
45         if not flag:
46             str = self.contents.get().upper()
47             self.contents.set(str)
48         else:
49             str = self.contents.get().lower()
50             self.contents.set(str)
51         print('the contents is : ', self.contents.get())
52
53     def print_contents(self, event):
54         print("hi. contents of entry is now ---->", self.contents.get())
55
56 root = App()
57 root.master.title("Foo")
58 root.mainloop()
  参考资料:
  http://www.oschina.net/code/explore/Python-3.1.3/Demo/tkinter/matt/entry-with-shared-variable.py

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60530-1-1.html 上篇帖子: Python 模块学习 logging(1) 下篇帖子: Python安装MySQLDb模块的种种问题及解决
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表