<p>I create a new Button object but did not specify the <code>command</code> option upon creation. Is there a way in Tkinter to change the command (onclick) function after the object has been created?</p>

[{'answer_id': 68455, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 2, 'selected': False, 'text': '<p>Sure; just use the <code>bind</code> 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 <a href="http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm" rel="nofollow noreferrer">http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm</a></p>\n\n<pre><code>from Tkinter import Tk, Button\n\nroot = Tk()\nbutton = Button(root, text="Click Me!")\nbutton.pack()\n\ndef callback(event):\n    print "Hello World!"\n\nbutton.bind("&lt;Button-1&gt;", callback)\nroot.mainloop()\n</code></pre>\n'}, {'answer_id': 68524, 'author': 'akdom', 'author_id': 145, 'author_profile': 'https://Stackoverflow.com/users/145', 'pm_score': 6, 'selected': True, 'text': '<p>Though <a href="https://stackoverflow.com/questions/68327/change-command-method-for-tkinter-button-in-python#68455">Eli Courtwright\'s</a> 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.</p>\n\n<pre><code>from Tkinter import Tk, Button\n\ndef goodbye_world():\n    print "Goodbye World!\\nWait, I changed my mind!"\n    button.configure(text = "Hello World!", command=hello_world)\n\ndef hello_world():\n    print "Hello World!\\nWait, I changed my mind!"\n    button.configure(text = "Goodbye World!", command=goodbye_world)\n\nroot = Tk()\nbutton = Button(root, text="Hello World!", command=hello_world)\nbutton.pack()\n\nroot.mainloop()\n</code></pre>\n\n<p>¹ "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 <code>command</code> option through <code>.configure</code> is much easier.</p>\n\n<p>² the only attribute that can\'t change after instantiation is <code>name</code>.</p>\n'}]
['https://Stackoverflow.com/questions/68327', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/680/']