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

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

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).

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...)

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.

how does a check button work in a Tkinter menu?

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.

Categories