在之前的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 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()
参考资料: