Tkinter checkbox executing command only once [duplicate] - python

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 1 year ago.
x = tk.IntVar()
def test():
print(x.get())
checkBox = tk.Checkbutton(text="test",command=test(),variable=x)
checkBox.grid(row=12,column=2)
b=tk.Button(text="test",command=test)
b.grid(row=12,column=0
this is some code I put in my program for testing, when I check the checkbox the value does change (I can confirm it with the button I made to test) but the command does not get executed.

Remove () in command
command= test
This works fine:
import tkinter as tk
win = tk.Tk()
x = tk.IntVar()
def test():
print(x.get())
checkBox = tk.Checkbutton(text="test", command=test, variable=x)
checkBox.grid(row=12, column=2)
win.mainloop()

Related

Getting an attribute error when referencing a Tkinter Button in its command function [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 12 months ago.
I have a class which is a Tkinter Frame, within it is a Tkinter Button. When I try to change a property of the button within its command function, I get the error AttributeError: 'ButtonsFrame' object has no attribute 'solve_button'.
The code is:
class ButtonsFrame(tk.Frame):
def __init__(self, container, puzzle_grid, options_bottom_frame):
super().__init__(container)
self.solve_button = tk.Button(self, text="Solve", font=(20), command=self.solve_button_clicked())
self.solve_button.grid(column=0, row=0, padx=5)
self.clear_button = tk.Button(self, text="Clear", font=(20), command=self.clear_button_clicked())
self.clear_button.grid(column=1, row=0, padx=5)
self.clear_button["state"] = "disabled"
self.grid(row=2, sticky="W", pady=5)
def solve_button_clicked(self):
# some deleted irrelevant code
if is_valid:
# some more irrelevant deleted code
self.solve_button["state"] = "disabled"
self.clear_button["state"] = "normal"
def clear_button_clicked(self):
reset_cell_text(cell_texts)
solve_button["state"] = "normal"
clear_button["state"] = "disabled"
Please help me with this error, as I imagine I'll have the same problem with clear_button.
Thanks!
The command should be a pointer to a function
In the code you wrote, the command gets the return value from the function.
command=self.solve_button_clicked()
The correct way is
command=self.solve_button_clicked

tkinter entry box updating before button is clicked [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 1 year ago.
I am writing a GUI application in python with tkinter. I have a function that is used in the button widget to change the text of an entry widget. When I run the python script, the entry widget updates with the text before I click the button widget. Can anyone tell me why it is doing this and how to fix it?
Please see my code below:
import tkinter as tk
def changeText(TKEntry, text):
TKEntry.insert(0, text)
def buildMain():
window = tk.Tk()
window.title("Login")
window.geometry("200x110")
lblLogin = tk.Label(window, text="Log In")
lblLogin.place(x=85,y=0)
lblUser = tk.Label(window, text="Username:")
lblUser.place(x=0,y=30)
edtUser = tk.Entry()
edtUser.place(x=70,y=30)
lblPass = tk.Label(window, text="Password:")
lblPass.place(x=0,y=50)
edtPass = tk.Entry()
edtPass.place(x=70,y=50)
btnLogin = tk.Button(text="Log In", command= changeText(edtUser,"Text Changed"))
btnLogin.place(x=150, y=80)
window.mainloop()
buildMain()
You need lambda:
command=lambda:changeText(edtUser,"Text Changed")
I think when the button was defined, the function was executed. That's why the entry was updated

how do I make a button in Tkinter that runs a shell command [duplicate]

This question already has answers here:
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
I am trying to make a button the command: print("test"). Here is what I have so far:
import os
root = tk.Tk()
button = tk.Button(text="print",
fg="black",
command=print("test")
)
button.pack()
root.mainloop()
how can I do this?
Following the solution to this question, the following should work:
import os
root = tk.Tk()
button = tk.Button(text="print",
fg="black",
command=lambda: print("test")
)
button.pack()
root.mainloop()

How to disable a button in tkinter? [duplicate]

This question already has answers here:
Disable / Enable Button in TKinter
(2 answers)
Closed 2 years ago.
When I click a button a new button displays but I want the previous clicked button to be disabled.
import tkinter as tk
def newbutton():
newbtn = tk.Button(app, text = "New Window button")
newbtn.pack()
app = tk.Tk()
buttonExample = tk.Button(app, text="Create new window", command=newbutton)
buttonExample.pack()
app.mainloop()
hi there have you tried to use the state command like this
buttonExample = tk.Button(app, text="Create new window", command=newbutton, state=DISABLED)
You can implement a very simple function that can disable a button
def disableButton(my_button):
my_button.config(state='disabled')
There is already a post on stack talking about that here

How to use Tk button? [duplicate]

This question already has answers here:
How to pass arguments to a Button command in Tkinter?
(18 answers)
Closed 3 years ago.
I'm a beginner in Python and I'm trying to build some GUI to understand.
I want to pass to a button a function that requies a parameter, but when I launch the script, it does not
work.
I'm attaching a python file.
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Hello World")
window.geometry('350x200')
def clicked(msg):
messagebox.showinfo("Message",msg)
text = Entry(window,width=100)
text.grid(column = 1, row = 0)
btn = Button(window,text = "Click me",command = clicked(text.get()))
btn.grid(column=5, row=1)
window.mainloop()
The following fix is required for your file:
When you assign the command parameter, it wont wait till your click on button, it will pop out the message box right after your tkinter application execution. (Thats what i experienced when i executed your code)
You have to use lambda here
So you can fix this by:
btn.bind('<Button-1>',lambda event: clicked('Your Text')) # Button-1 stands for left mouse click
More on bind() method:
https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
This is the final code:
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Hello World")
window.geometry('350x200')
def clicked(msg):
messagebox.showinfo("Message",msg)
text = Entry(window,width=100)
text.grid(column = 1, row = 0)
btn = Button(window,text = "Click me")
btn.bind('<Button-1>',lambda event: clicked(text.get()))
btn.grid(column=5, row=1)
window.mainloop()
The command will be executed when the code is interpreted, to avoid that you can use lambda to pass an argument to the function
command = lambda : clicked(text.get()))
Or you can use partial which will return a callable object that behaves like a function when it is called.
from functools import partial
...
command = partial(clicked, text.get()))

Categories

Resources