| 1 | from Tkinter import * |
| 2 | import string |
| 3 | |
| 4 | # This program shows how to use a simple type-in box |
| 5 | |
| 6 | class App(Frame): |
| 7 | def __init__(self, master=None): |
| 8 | Frame.__init__(self, master) |
| 9 | self.pack() |
| 10 | |
| 11 | self.entrythingy = Entry() |
| 12 | self.entrythingy.pack() |
| 13 | |
| 14 | # and here we get a callback when the user hits return. we could |
| 15 | # make the key that triggers the callback anything we wanted to. |
| 16 | # other typical options might be <Key-Tab> or <Key> (for anything) |
| 17 | self.entrythingy.bind('<Key-Return>', self.print_contents) |
| 18 | |
| 19 | # Note that here is where we bind a completely different callback to |
| 20 | # the same event. We pass "+" here to indicate that we wish to ADD |
| 21 | # this callback to the list associated with this event type. |
| 22 | # Not specifying "+" would simply override whatever callback was |
| 23 | # defined on this event. |
| 24 | self.entrythingy.bind('<Key-Return>', self.print_something_else, "+") |
| 25 | |
| 26 | def print_contents(self, event): |
| 27 | print "hi. contents of entry is now ---->", self.entrythingy.get() |
| 28 | |
| 29 | |
| 30 | def print_something_else(self, event): |
| 31 | print "hi. Now doing something completely different" |
| 32 | |
| 33 | |
| 34 | root = App() |
| 35 | root.master.title("Foo") |
| 36 | root.mainloop() |
| 37 | |
| 38 | |
| 39 | |
| 40 | # secret tip for experts: if you pass *any* non-false value as |
| 41 | # the third parameter to bind(), Tkinter.py will accumulate |
| 42 | # callbacks instead of overwriting. I use "+" here because that's |
| 43 | # the Tk notation for getting this sort of behavior. The perfect GUI |
| 44 | # interface would use a less obscure notation. |