前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python开发_tkinter_获取文本框内容_给文本框添加键盘输入事件

python开发_tkinter_获取文本框内容_给文本框添加键盘输入事件

作者头像
Hongten
发布2018-09-13 12:08:25
3K0
发布2018-09-13 12:08:25
举报
文章被收录于专栏:HongtenHongten

在之前的blog中有提到python的tkinter中的菜单操作

下面是tkinter的获取文本框内容_给文本框添加键盘输入事件的操作

运行效果:

在输入的时候,单击回车键,触发:print_contents()

==========================================================

代码部分:

==========================================================

 1 from tkinter import *
 2 
 3 __author__ = {'name' : 'Hongten',
 4               'mail' : 'hongtenzone@foxmail.com',
 5               'blog' : 'http://www.cnblogs.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 <Key-Tab> or <Key> (for anything)
35         self.entrythingy.bind('<Key-Return>', 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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档