When you create a binding with bind
, Tkinter automatically adds an argument that has information about the event. You'll need to account for that either in your rand_func
definition or in how you call it.
This argument is not included when you use the command
attribute. You must take care to account for this extra argument either in how you call the function in each case, or in how the function interprets its parameters.
Here's one solution that uses lambda
in the binding to accept the extra event only when using bind
command, but not pass it on to the final command.
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frame = tk.Frame(self)
self.frame.pack()
self.button = tk.Button(self.frame, text="click me",
command=lambda a=1, b=2, c=3:
self.rand_func(a, b, c))
self.button.pack()
self.frame.bind("<Return>",
lambda event, a=10, b=20, c=30:
self.rand_func(a, b, c))
# make sure the frame has focus so the binding will work
self.frame.focus_set()
def rand_func(self, a, b, c):
print "self:", self, "a:", a, "b:", b, "c:", c
print (a+b+c)
app = SampleApp()
app.mainloop()
That being said, it's rare that binding to a frame is the right thing to do. Typically a frame won't have keyboard focus, and unless it has focus the binding will never fire. If you are setting a global binding you should either bind to the "all" binding tag (using the bind_all method) or to the toplevel widget.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…