简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM