I've tried the code below to block a button, because if it is clicked while the def start is working it blows whole app. The button calls both defs start and block, although function start is not working at all.
The problem is that I can't put button_start.config(state=tk.DISABLED)
in def start():
, because it changes every 1000 ms and the button is weirdly pulsating.
I've searched it and that is my idea to deal with it. I'm not a pro coder and this might be stupid so I am counting on your expirience.
root = tk.Tk()
def stop_app():
button_start.config(state=tk.NORMAL)
def start():
#something working here...
root.after(1000, start)
def block():
button_start.config(state=tk.DISABLED)
button_start = tk.Button( root, command=start and block)
button_start.place(x=250, y=235)
button_stop = tk.Button(root, command=stop_app)
button_stop.place(x=305, y=235)
CodePudding user response:
The line,
button_start = tk.Button(root, command=start and block)
has an error, command=start and block
. You can’t have 2 functions for the same button.
What you can do instead of this is:
def block():
button_start.config(state=tk.DISABLED)
def start():
block()
Or the same thing but with lesser number of lines:
def start():
button_start.config(tk.DISABLED)
And change the line with the error to:
button_start = tk.Button(root, command=start)
CodePudding user response:
I think you may be making things too complicated. The code below simply toggles the states of the two Button
s depending on the state of the first one so they are always the opposite of one another.
import tkinter as tk
root = tk.Tk()
root.geometry('350x350')
def start_app():
if button_start.cget('state') == tk.NORMAL:
button_start.config(state=tk.DISABLED)
button_stop.config(state=tk.NORMAL)
def stop_app():
if button_stop.cget('state') == tk.NORMAL:
button_stop.config(state=tk.DISABLED)
button_start.config(state=tk.NORMAL)
button_start = tk.Button(root, text='Start', command=start_app, state=tk.NORMAL)
button_start.place(x=250, y=235)
button_stop = tk.Button(root, text='Stop', command=stop_app, state=tk.DISABLED)
button_stop.place(x=305, y=235)
root.mainloop()
Screenshots showing before clicking on the Start button and after doing so: