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?
Asked by: Aston588 | Posted: 28-01-2022
Answer 1
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
.
Answer 2
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()
Answered by: Lyndon426 | Posted: 01-03-2022
Similar questions
user interface - Why my button will not repeat command with tkinter GUI on python
I'm new to GUI and classes and I'm a just a bit confused, when I use a button in tkinter for python it's suppose to repeat it's command when pressed. but in my program it doesn't do that. is there something wrong with me codes that might counter it? I'm trying to make a simple program that echos whatever is typed.
-Thanks
from Tkinter import *
from PIL import Image, ImageTk
import tkMessageBox
clas...
How to pass a string as stdin to python script from command line interface
Currently, I am using the following command to do this
$ python scriptName.py <filePath
This command uses "<" to stdin the file to script.
and it works fine, I can use sys.stdin.read to get the file data.
But, what if I want to pass file data as a string,
I don't want to pass file path in operator "<".
Is there is anyway, where I can pass String as stdin to a python s...
user interface - Why my button will not repeat command with tkinter GUI on python
I'm new to GUI and classes and I'm a just a bit confused, when I use a button in tkinter for python it's suppose to repeat it's command when pressed. but in my program it doesn't do that. is there something wrong with me codes that might counter it? I'm trying to make a simple program that echos whatever is typed.
-Thanks
from Tkinter import *
from PIL import Image, ImageTk
import tkMessageBox
clas...
interface - Using python to issue command prompts
I have been teaching myself python over the past few months and am finally starting to do some useful things.
What I am trying to ultimately do is have a python script that acts as a queue. That is, I would like to have a folder with a bunch of input files that another program uses to run calculations (I am a theoretical physicist and do many computational jobs a day).
The way I must do this now is put all...
user interface - Command prompt messed up after running a Python program
The code below creates a layout and displays some text in the layout. Next the layout is displayed on the console screen using the raw display module from the Urwid library. (More information on my complete project can be gleaned from questions at
python - Whats a better way to code a command line interface in c++?
I'm using run of the mill C++, and im building a small "Zork-esc" game as a pet project to exercise my newly learned C++ skills. the following code works perfectly, however it will be a pain to have to do this for every command/argument combo so if anybody out there can save me some trouble then please do :D ...
as it stands i have this function responsible for processing commands at run-time...
...
linux - python command line interface with shell prompt
I am researching into a python CLI application with a shell prompt. To clarify, I am looking for something which supports 2 functionalities.
1) provides a shell prompt & auto complete on the commands like how "cmd" module supports. for eg: the "do_" prefixed commands. ( do_xyz: do_abc. )
2) provides support for multiple sub-commands and options. for eg:
$ python my_prog_prompt.py
(my_p...
command line interface - Python click - allow a prompt to be empty
I'm using the click library for a CLI application. I have various options that the user can specify, and most of them have prompts turned on. However, even if an option isn't required, if you set click to prompt for the option, it won't accept an empty response (like just hitting enter). For example:
@click.option('-n', '--name', required=True, prompt=True)
@click.option('-d', '--description', required=Fals...
django - How to pass user input to os command line interface using Python?
I am passing some user input to views.py file and need to pass those input param to OS command line interface using Python. I am providing my code below:
rname = request.POST.get('react')
if 'strt' in request.POST:
status = 1
if 'shutbt' in request.POST:
status = 0
if 'susbtn' in request.POST:
status = 2
passw = Reactor(
...
command line interface - Python Click Module Dict Return Value
In the documentation the usage of the click module is this way:
@click.command()
@click.argument('tgt')
@click.argument('fun')
def main(tgt, fun):
#stuff here
How can I pass a dictionary which contains all arguments that need to be passed to the main function instead of passing each argument separately, i.e:
@click.command()
@click.argument('tgt')
@click.argument('fun')...
user interface - run command from Python and then close
I am trying to build a small shell application, which runs a command from a GUI application. When I press the "off" button, the console for the command should be closed. I have two questions:
When I run the command, the command is running on the same console as my Python script. How can I open the command in a new console?
How can I stop the command? I mean like if the command is working in...
python - In the Django admin interface, is there a way to duplicate an item?
Just wondering if there is an easy way to add the functionality to duplicate an existing listing in the admin interface?
In data entry we have run into a situation where a lot of items share generic data with another item, and to save time it would be very nice to quickly duplicate an existing listing and only alter the changed data. Using a better model structure would be one way of reducing the duplication of the...
user interface - making a python GUI
This question already has answers here:
Python file interface for strings
This question already has answers here:
user interface - Are there any good 3rd party GUI products for Python?
In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite.
Edit 01-16-09: For Example:
http://www.devexpress.com/Downloads/NET/OnlineDemos.xml
user interface - Is there a Python library for easily writing zoomable UI's?
My next work is going to be heavily focused on working with data that is best understood when organized on a two-dimensional zoomable plane or canvas, instead of using lists and property forms.
The library can be based on OpenGL, GTK+ or Cairo. It should allow me to:
build widgets out of vector shapes and text (perhaps even SVG based?)
arrange these widgets on a 2D plane
catch widget...
user interface - How to modify existing panels in Maya using MEL or Python?
I've been writing tools in Maya for years using MEL and Python. I'd consider myself an expert in custom window/gui design in Maya except for one area; modifying existing panels and editors.
Typically, I'm building tools that need totally custom UIs, so its customary for me to build them from scratch. However, recently I've found myself wanting to add some additional functionality to the layer editor in Maya. I'v...
user interface - What library is best for GUI in python?
This question already has answers here:
user interface - Where can I find a GUI designer for Python?
python - What SHOULDN'T Django's admin interface be used for?
I've been applying Django's automatic administration capabilities to some applications who had previously been very difficult to administer. I'm thinking of a lot of ways to apply it to other applications we use (including using it to replace some internal apps altogether). Before I go overboard though, is there anything in particular I shouldn't use it for?
Source interface with Python and urllib2
How do i set the source IP/interface with Python and urllib2?
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python