Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from Tkinter import * |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 2 | import string |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 3 | |
| 4 | # This program shows how to make a typein box shadow a program variable. |
| 5 | |
| 6 | class App(Frame): |
| 7 | def __init__(self, master=None): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 8 | Frame.__init__(self, master) |
| 9 | self.pack() |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 10 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 11 | self.entrythingy = Entry(self) |
| 12 | self.entrythingy.pack() |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 13 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 14 | self.button = Button(self, text="Uppercase The Entry", |
| 15 | command=self.upper) |
| 16 | self.button.pack() |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 17 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 18 | # 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") |
| 25 | self.entrythingy.config(textvariable=self.contents) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 26 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 31 | |
| 32 | def upper(self): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 33 | # notice here, we don't actually refer to the entry box. |
| 34 | # we just operate on the string variable and we |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 35 | # because it's being looked at by the entry widget, changing |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 40 | |
| 41 | def print_contents(self, event): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 42 | print("hi. contents of entry is now ---->", self.contents.get()) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 43 | |
| 44 | root = App() |
| 45 | root.master.title("Foo") |
| 46 | root.mainloop() |