how does a check button work in a Tkinter menu? - python

goal
To understand how the check button works in a Tkinter menu. Especially how the value of the variable associated is changed and when the function mentioned in the command is called.
code
I have the following checkbutton that I have added to a Tkinter menu:
window = Tk()
shown = BooleanVar()
shown.set(True)
menubar = Menu(window)
optionsmenu = Menu(menubar,tearoff=0)
optionsmenu.add_checkbutton(label='Show timing after the run is completed',command=PopUp,variable=shown,onvalue = True,offvalue = False)
For simplicity the on value of the check button is true and the off value is false.
what I want to know:
Is the value of the variable changed when the check button is pressed or is the function called and the value of the variable needs to be changed explicitly?
Is the command executed before the variable is toggled or after it has been toggled??
specs
Windows XP SP3
Python 2.7
Please help me with this doubt.

The answers to your questions are as so:
Yes the variable is changed when the check button is pressed. That is the normal behavior of the check button widget.
The command is called after the value of the variable has been toggled from on to off or vice versa whatsoever be the case.

Related

tkinter button only visible when a word is in a textbox python

I'm trying to create a button in Tkinter that only appears (becoming visible) when it finds the word "hello" in a textbox.
I could imagine using Threads, and Global variables but i do not know how to code it,
I've imagined something like :
import Tkinter as *
from threading import Thread
window = Tk()
active = True
def check_for_word():
global active
textbox1 = textbox.get("1,0", "end")
while active == True:
if "hello" in textbox1:
button.pack()
else:
button.pack_forget()
save_button = Button(window)
textbox = scrolledtext.ScrolledText(window)
textbox.pack()
threading = Thread (target=check_for_word)
threading.start()
window.mainloop()
this is something I would suspect to work but ends up not, the button either doesn't show at all like the code isn't even running, or the thread doesn't work properly. So am I doing something wrong, if so, can you help me, please? Thank you!
You don't need to make use of threads to do this, you can use tkinter event bindings instead.
def check_for_word():
if "hello" in textbox.get("1.0", "end"):
save_button.pack()
else:
save_button.pack_forget()
save_button = Button(window)
textbox = scrolledtext.ScrolledText(window)
textbox.bind("<KeyRelease>", lambda event:check_for_word())
textbox.pack()
To make a binding, you use widget.bind. In this case, the widget is textbox and it's binding to <KeyRelease>, which is when the user releases a key. It then calls check_for_word when a key is released. (The lambda part is to ignore the event parameter). check_for_word then does what it did before.
You have to put the textbox1 assignment inside the while loop and before the if condition, otherwise it will check the value one time before entering the loop and will keep checking always the same value.
I also want to point out that the in operator is case sensitive and return True if it find even just a substring inside the variable you are checking and not just the precise single word (but I'm not shure if this is intensional).
For the while loop you don't necessarily need a global variable, you could just use while True: if you want it to continuously check the condition (if you want the button to disappear after the user cancel the word).

Python tkinter Checkbutton not appearing selected in Topframe window

When programming with tkinter I have found a very strange behaviour of the Checkbutton widget. I have re-created the bug with the code below:
import tkinter
from tkinter import *
def displayWelcomeScreen(root):
root2 = Toplevel(root)
root2.geometry('600x380')
root2.focus_set()
Checked = IntVar()
CheckButton1 = Checkbutton(root2, variable=Checked)
CheckButton1.place(relx=0.5, rely=0.5, anchor=CENTER)
CheckButton1.select()
# Create a dummy button that makes the Checkbutton appear checked to the user
#Button(root2, command= lambda event: Checked.get())
root = Tk()
root.geometry('700x400')
displayWelcomeScreen(root)
root.mainloop()
When a new window is created with Toplevel(root) and I put a Checkbutton inside it, it does not appear checked to the user even though I use the .select() method.
However, when I create a dummy button whose command mentions the IntVar associated with my Checkbutton, somehow it is initialised as checked properly. It's almost as if the compiler checks whether the Checkbutton will be useful and decides based on that whether it will display it as selected or not.
EDIT: The Checkbutton is definitely checked under the hood because if I run print(Checked.get()) before and after the CheckButton1.select() command, the value is changed, it just doesn't appear to the user.
Does anyone know why this happens?
EDIT 2: Thanks to jasonharper's explanation, I have added the line CheckButton1.intvar = Checked and it worked without needing the dummy button. When the function went out of scope, the Checked variable got lost so the Checkbutton had nowhere to store its state, therefore we needed to keep a reference to it so it didn't disappear.

how to check if button is clicked on tkinter

I am trying to create a car configurator using tkinter as a gui in my free time.
I have managed to open a tkinter box with images that act as buttons.
What I want to do is for the user to click on a button. I want to check which button has been clicked (i.e if the family car button is clicked, how can I check that it has been clicked).
I have done my research on this website, and all of the solutions I have found have been in javascript or other languages.
Once the button has been clicked, I want a new window to be opened ONLY containing attributes for a family car i.e a family car can have a red exterior colour, but a sports car cannot have a red exterior colour at all.
Here is my code below:
from tkinter import *
import tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
familycar = PhotoImage(file = "VW family car.png")
familylabel = Button(root, image=familycar)
familybutton = Button(root, image=familycar, command=create_window)
familybutton.pack()
So how can I check that the family car button has been clicked?
Thanks
Use a Boolean flag.
Define isClicked as False near the beginning of your code, and then set isClicked as True in your create_window() function.
This way, other functions and variables in your code can see whether the button's been clicked (if isClicked).
Not sure what you asked, do you want to disable it or check its status in another routine ?
Or just to count the times it has been clicked,
In order to do that Simple solution would be to add a general variable that will be updated inside the create_window method (general because you want to allow access from other places).
First, you would need to initialize a function in which you want to execute on the button click.
example:
def button_clicked():
print('I got clicked')
Then, when you define the target button, you'll have to set the 'command' argument to the required function(the button_clicked function in this context).

Enabling a checkbox Tkinter (Python 3.4)

I have a CheckBox in Tkinter. I want it to always stay checked but disabling the checkbox destroys the looks of the GUI application. I want to keep its state as Normal and if a user tries to uncheck it the box remains checked, or rechecks itself immediately after.
global ghistory
ghistory = IntVar()
cc = Checkbutton(frame3, text="History", variable=ghistory)
cc.select()
cc.pack()
How do I do it?
Add a function that sets the variable to True. A quick lambda function would do the trick:
cc=tk.Checkbutton(frame3,text="History",variable=ghistory, command=lambda:ghistory.set(1))
Or you could use the select command:
cc=tk.Checkbutton(frame3,text="History",variable=ghistory)
cc['command'] = cc.select
use .cget('state') to see if the button is disabled...
self.widget_checkbutton = tk.Checkbutton(self, variable=self.some_variable, command=lambda:self.stay_checked())
def stay_checked(self):
if self.widget_checkbutton.cget('state') == 'disabled':
self.widget_checkbutton.select()
else:
self.widget_checkbutton.deselect()
#this will only let the button be checked if widget is active.
(so basically, if some widget is disabled... you tell the checkbox to stay the same. else if some widget is normal, you allow the checkbox to work normally... select... deselect...)

How to disable/enable a specific tkinter button under certain conditions?

I have created the following button in a tinter window:
resetall = Button(text = "Clear ALL", command = confirmation)
resetall.pack(side = "left")
This button "Clears" the canvas that the user is drawing on with the Python turtle, but I want this button to be enabled under CERTAIN CONDITIONS, such as if one function is running or not. I have tried this:
if draw.drawing == False:
resetall.config(state = DISABLED)
elif draw.drawing == True:
resetall.config(state = NORMAL)
to enable the button ONLY when the "draw" function is true, otherwise disable it. However, it does not seem to work, as even when the draw function becomes true, it does not get enabled. What am I doing wrong here? Any help is much appreciated! :)
Twas a very simple fix. All I had to do was make resetall a global variable, and then assign resetall.config(state = ACTIVE) to draw.
Make the variable containing a button to be a global variable and then the state of the button can be changed by using button_name.config(state=ACTIVE) or button_name.config(state=DISABLED).
Remember- once the button has been disabled you will not be able to activate it if you have changed the state of the button to disabled inside that same function. You would need another function to activate your previous button once disabled

Categories