bind-w-mult-calls-p-type.py 1.52 KB
Newer Older
1
from Tkinter import *
2
import string
3 4 5 6 7

# This program  shows how to use a simple type-in box

class App(Frame):
    def __init__(self, master=None):
8 9
        Frame.__init__(self, master)
        self.pack()
10

11 12
        self.entrythingy = Entry()
        self.entrythingy.pack()
13

14 15 16 17
        # and here we get a callback when the user hits return. we could
        # make the key that triggers the callback anything we wanted to.
        # other typical options might be <Key-Tab> or <Key> (for anything)
        self.entrythingy.bind('<Key-Return>', self.print_contents)
18

19 20 21 22 23 24
        # Note that here is where we bind a completely different callback to
        # the same event. We pass "+" here to indicate that we wish to ADD
        # this callback to the list associated with this event type.
        # Not specifying "+" would simply override whatever callback was
        # defined on this event.
        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 35 36 37 38 39


root = App()
root.master.title("Foo")
root.mainloop()



40 41
# secret tip for experts: if you pass *any* non-false value as
# 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.