Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change command Method for Tkinter Button in Python

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 object has been created?

like image 934
Cristian Avatar asked Sep 16 '08 00:09

Cristian


People also ask

Can you change the command of a button Tkinter?

You can change the “text” property of the Tkinter button by using the button reference and the “text” option as an index. Now 'value' contains the button text.

How do you command a button in Python?

The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.

Can a Tkinter button have multiple commands?

The Tkinter button has only one command property so that multiple commands or functions should be wrapped to one function that is bound to this command .

How does command work in Tkinter?

In Tkinter, some widgets allow you to associate a callback function with an event using the command binding. It means that you can assign the name of a function to the command option of the widget so that when the event occurs on the widget, the function will be called automatically.


2 Answers

Though Eli Courtwright's program will work fine¹, what you really seem to want though is just a way to reconfigure after instantiation any attribute which you could have set when you instantiated². How you do so is by way of the configure() method.

from Tkinter import Tk, Button

def goodbye_world():
    print "Goodbye World!\nWait, I changed my mind!"
    button.configure(text = "Hello World!", command=hello_world)

def hello_world():
    print "Hello World!\nWait, I changed my mind!"
    button.configure(text = "Goodbye World!", command=goodbye_world)

root = Tk()
button = Button(root, text="Hello World!", command=hello_world)
button.pack()

root.mainloop()

¹ "fine" if you use only the mouse; if you care about tabbing and using [Space] or [Enter] on buttons, then you will have to implement (duplicating existing code) keypress events too. Setting the command option through .configure is much easier.

² the only attribute that can't change after instantiation is name.

like image 163
akdom Avatar answered Sep 19 '22 08:09

akdom


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("<Button-1>", callback)
root.mainloop()
like image 30
Eli Courtwright Avatar answered Sep 18 '22 08:09

Eli Courtwright