blob: 7d93da764e03c08742cb1be13a0f53e4acc1f32d [file] [log] [blame]
Benjamin Petersond6d63f52009-01-04 18:53:28 +00001from tkinter import *
Guido van Rossum35820f71994-10-07 09:55:26 +00002
3# This program shows how to make a typein box shadow a program variable.
4
5class App(Frame):
6 def __init__(self, master=None):
Tim Peters182b5ac2004-07-18 06:16:08 +00007 Frame.__init__(self, master)
8 self.pack()
Guido van Rossum35820f71994-10-07 09:55:26 +00009
Tim Peters182b5ac2004-07-18 06:16:08 +000010 self.entrythingy = Entry(self)
11 self.entrythingy.pack()
Guido van Rossum35820f71994-10-07 09:55:26 +000012
Tim Peters182b5ac2004-07-18 06:16:08 +000013 self.button = Button(self, text="Uppercase The Entry",
14 command=self.upper)
15 self.button.pack()
Guido van Rossum35820f71994-10-07 09:55:26 +000016
Tim Peters182b5ac2004-07-18 06:16:08 +000017 # here we have the text in the entry widget tied to a variable.
18 # changes in the variable are echoed in the widget and vice versa.
19 # Very handy.
20 # there are other Variable types. See Tkinter.py for all
21 # the other variable types that can be shadowed
22 self.contents = StringVar()
23 self.contents.set("this is a variable")
24 self.entrythingy.config(textvariable=self.contents)
Guido van Rossum35820f71994-10-07 09:55:26 +000025
Tim Peters182b5ac2004-07-18 06:16:08 +000026 # and here we get a callback when the user hits return. we could
27 # make the key that triggers the callback anything we wanted to.
28 # other typical options might be <Key-Tab> or <Key> (for anything)
29 self.entrythingy.bind('<Key-Return>', self.print_contents)
Guido van Rossum35820f71994-10-07 09:55:26 +000030
31 def upper(self):
Tim Peters182b5ac2004-07-18 06:16:08 +000032 # notice here, we don't actually refer to the entry box.
33 # we just operate on the string variable and we
Guido van Rossum35820f71994-10-07 09:55:26 +000034 # because it's being looked at by the entry widget, changing
Tim Peters182b5ac2004-07-18 06:16:08 +000035 # the variable changes the entry widget display automatically.
36 # the strange get/set operators are clunky, true...
Georg Brandl856023a2010-10-25 17:50:20 +000037 str = self.contents.get().upper()
Tim Peters182b5ac2004-07-18 06:16:08 +000038 self.contents.set(str)
Guido van Rossum35820f71994-10-07 09:55:26 +000039
40 def print_contents(self, event):
Collin Winter6f2df4d2007-07-17 20:59:35 +000041 print("hi. contents of entry is now ---->", self.contents.get())
Guido van Rossum35820f71994-10-07 09:55:26 +000042
43root = App()
44root.master.title("Foo")
45root.mainloop()