How to execute python tkinter button without clicking mouse? - python

I build a GUI application, there is some button and they perform some task. Now i want to perform that mouse click with out clicking that mouse. like with the help of any int number. like if i put a=1 than button 1 will clicked if i put b=2 than button 2 will clicked. How can i do that?

To press the button use its invoke method.
button.invoke()

In addition to Robert's answer of using the invoke method, you can also directly call the button's function.
Assume the button is defined as follows:
button = Button(master, text="Hello World", command=callback)
Then, you can just call the function callback with callback().

Related

Moving TKinter Window.destroy to a function from a button, not so simple?

I'm giving myself a crash-course in Python and TKinter, but there is one small detail I can't grasp.
Closing a Toplevel window in a function instead of a button.
My button alone works perfect:
button = Button(UpdateWindow, text="Destroy Window", command=UpdateWindow.destroy)
Using a button with a reference to a close function bombs:
def Close():
tkMessageBox.showwarning('', 'Close function called', icon="warning")
command=UpdateWindow.destroy
btn_updatecon = Button(ContactForm, text="Update", width=20, command=lambda:[UpdateData(), Close()])
What am I missing in the function? It is being called, but no close.
The SQLite3 project im working with is here
Any guidance greatly appreciated.
command=UpdateWindow.destroy defines a variable. If you want to keep the command variable use command(), while if you want the recommended way use UpdateWindow.destroy()

Is it possible to destroy a button or checkbox when it is clicked?

I am currently using tkinter to build a GUI and one of the functionalities I was hoping to achieve with the buttons was if it can be destroyed when it is clicked. I tried something along the lines of:
button = Button(window, text="hello", command=button.destroy()
This doesn't work as I'm getting the error:
UnboundLocalError: local variable 'button' referenced before assignment.
Are there are workarounds to accomplish a task like this?
You need to save a reference to the button, and then call the destroy method on the button. Here's one way:
button = Button(window, text="hello")
button.configure(command=button.destroy)
The problem in your code is that you're trying to use button before the button has been created. Creating the button and then configuring the button in a separate step works around that problem since button exists by the time the second line is called.
Also notice that command is set to button.destroy, not button.destroy(). With the parenthesis, you're going to immediately call the method instead of assigning the command to the button.
That depends.. there is the .grid_forget method (or .pack_forget using pack instead of grid) if you don't want to delete permanently the widget (you can re-add it with .grid), and also the .grid_remove or pack.remove, but otherwise I suggest you to use .destroy().

Call a function after pressing a button (tkinter) [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I created a button "button" and I want to call a function "function" when I press on this button.
When I run the program, the function is called and then the button appears. If I press it once again, the function is not called. So how to disable calling the function without pressing the button and how to enable calling the function at each press on the butotn?
from tkinter import *
def function():
what this function does
root = Tk()
button = Button(root,text="Call function", command=function())
button.pack()
root.mainloop()
You need to pass the function (don't call it):
button = Button(root,text="Call function", command=function)
I think you have to remove the brackets from the function, because you just want to give the function as a parameter to your button and you do not want to call the function instead.
So it will look like:
button = Button(root,text="Call function", command=function)

Define pressed button of drop-down list in Tkinter Python

I created a drop-down list using Menubutton from Python Tkinter, but i can't detect which button was pressed ('button-1', 'button-2' or 'button-3')
from Tkinter import *
widget = Frame()
widget.pack()
btnMenu = Menubutton(widget, text='Select action')
contentMenu = Menu(btnMenu)
btnMenu.config(menu=contentMenu)
btnMenu.pack()
btnList = ['button-1', 'button-2', 'button-3']
for btn in btnList:
contentMenu.add_command(label=btn, command=???)
mainloop()
What should i use for "command=" in the string
contentMenu.add_command(label=btn, command=???)
in order to define particular button? Thank you!
What you're looking for is lambda. You can use lambda in your command call like such:
contentMenu.add_command(label=btn, command = lambda btn=btn: buttonClicked(btn))
Then make a method called buttonClicked which would take one argument which would reflect which button has been pressed. Here's a minimal example of what that would look like:
def buttonClicked(btn):
print btn
Ideally though if each button has an entirely different set of execution instructions then they should each get their own method and perhaps you change the list to a tuple of (name, method). This is usually the case for why you would use a menubutton instead of an optionmenu. If you're simply calling the same method for all of them then you might want to consider switching to an optionmenu instead.

Is there a way to press a button without touching it on tkinter / python?

Hi I need to do this because, I am making a matching / memmory game, and there has to be a button (Totally separated from the ones on the current game) that when I press it, it has to show the matching cards automatically without having to touch the buttons with the mouse.
Is there a "press" function or something like that for pressing the button?
Thanks! :)
As Joel Cornett suggests in a comment, it might make more sense to simply call the callback that you passed to the button. However, as described in the docs, the Button.invoke() method will have the same effect as pressing the button (and will return the result of the callback), with the slight advantage that it will have no effect if the button is currently disabled or has no callback.
If you also want visual feedback for the button you can do something like this:
from time import sleep
# somewhere the button is defined to do something when clicked
self.button_save = tk.Button(text="Save", command = self.doSomething)
# somewhere else
self.button_save.bind("<Return>", self.invoke_button)
def invoke_button(self, event):
event.widget.config(relief = "sunken")
self.root.update_idletasks()
event.widget.invoke()
sleep(0.1)
event.widget.config(relief = "raised")
In this example when the button has focus and Enter/Return is pressed on the keyboard, the button appears to be pressed, does the same thing as when clicked (mouse/touch) and then appears unpressed again.

Categories