blob: 360f97373b3eabea076782702fbc4b566360e9c8 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2import string
3
4# This program shows how to make a typein box shadow a program variable.
5
6class App(Frame):
7 def __init__(self, master=None):
8 Frame.__init__(self, master)
9 self.pack()
10
Guido van Rossum58a78561996-11-27 19:47:42 +000011 self.entrythingy = Entry(self)
Guido van Rossum35820f71994-10-07 09:55:26 +000012 self.entrythingy.pack()
13
Guido van Rossum89cb67b1996-07-30 18:57:18 +000014 self.button = Button(self, text="Uppercase The Entry",
15 command=self.upper)
Guido van Rossum35820f71994-10-07 09:55:26 +000016 self.button.pack()
17
Guido van Rossum35820f71994-10-07 09:55:26 +000018 # here we have the text in the entry widget tied to a variable.
19 # changes in the variable are echoed in the widget and vice versa.
20 # Very handy.
21 # there are other Variable types. See Tkinter.py for all
22 # the other variable types that can be shadowed
23 self.contents = StringVar()
24 self.contents.set("this is a variable")
Guido van Rossum89cb67b1996-07-30 18:57:18 +000025 self.entrythingy.config(textvariable=self.contents)
Guido van Rossum35820f71994-10-07 09:55:26 +000026
27 # and here we get a callback when the user hits return. we could
28 # make the key that triggers the callback anything we wanted to.
29 # other typical options might be <Key-Tab> or <Key> (for anything)
30 self.entrythingy.bind('<Key-Return>', self.print_contents)
31
32 def upper(self):
33 # notice here, we don't actually refer to the entry box.
34 # we just operate on the string variable and we
35 # because it's being looked at by the entry widget, changing
36 # the variable changes the entry widget display automatically.
37 # the strange get/set operators are clunky, true...
38 str = string.upper(self.contents.get())
39 self.contents.set(str)
40
41 def print_contents(self, event):
42 print "hi. contents of entry is now ---->", self.contents.get()
43
44root = App()
45root.master.title("Foo")
46root.mainloop()
47