Change command Method for Tkinter Button in Python

前端 未结 2 1087
执笔经年
执笔经年 2020-12-16 15:57

I create a new Button object but did not specify the command option upon creation. Is there a way in Tkinter to change the command (onclick) function after the

2条回答
  •  旧时难觅i
    2020-12-16 16:26

    Sure; just use the bind method to specify the callback after the button has been created. I've just written and tested the example below. You can find a nice tutorial on doing this at http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm

    from Tkinter import Tk, Button
    
    root = Tk()
    button = Button(root, text="Click Me!")
    button.pack()
    
    def callback(event):
        print "Hello World!"
    
    button.bind("", callback)
    root.mainloop()
    

提交回复
热议问题