How to bind the root to call method inside a class
This is the basic layout of my program:
class App(CheckInfo):
def __init__(self, master):
CheckInfo.__init__(self)
master.title("Example")
.....
After that I have i method (inside the class App) that goes like this:
def moveIt(self):
print "It doesnt work"
At the bottom (outside if the class) I have:
root = Tk()
app = App(root)
#root.bind("<Up>",) # I don't know 开发者_如何学运维how this works
root.mainloop()
I am trying to make a bind to the root
so that at any moment in the window I can press the up key an call the method.
I'm not sure how this is done.
My guesses so far have not made much progress.
I think I might need to put event in: moveIt(self)
=> moveIt(self,event)
But I have no idea how to put the method as an argument in the bind since:
root.bind("<Up>",moveIt) #doesnt work
root.bind("<Up>",self.moveIt) #obviously not
root.bind("<Up>",root.moveIt) #donsnt make much sense
Any ideas would be appreciated! I hope I have posted all the relevant code, otherwise please ask for any needed clarification.
Thanks in advance.
Your experiments may not be working because a frame by default does not have the keyboard focus. Try adding root.focus() so that keyboard events are directed to the root window.
The other part of your question has to do with how to do the binding. Since moveIt is a method of the class App and 'app' is an instance of that class, what you want is:
root.bind("<Up>", app.moveIt)
精彩评论