Reputation: 855
I have a tkinter application with 2 buttons. Run application
and Store data
. I don't want to give the end-user the opportunity to click Store data
if he has not clicked Run application
first.
So the button Store data
needs to get un-disabled after the button Run application
has been clicked first.
In my code below I have created a checker
function which is supposed to do above, however the Store data
button always stays disabled.
This is my code:
import os
from tkinter import *
owner = ['Spain', 'United Kingdom', 'Malaysia']
path_spain = r"c:\data\FF\Desktop\PythonFolder\spain_software.py"
path_uk = r"c:\data\FF\Desktop\PythonFolder\uk_software.py"
path_malaysia = r"c:\data\FF\Desktop\PythonFolder\malaysia_software.py"
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
data = []
#Confirmation function
def store():
data.append(dd_owner.get())
print(data)
checker()
def run():
if dd_owner.get() == "Spain":
print("spain")
# os.system('python path_spain')
elif dd_owner.get() == "United Kingdom":
os.system('python path_uk')
elif dd_owner.get() == "Malaysia":
os.system('python path_malaysia')
def update_button(_):
run_button.config(text="Run application {}".format(dd_owner.get()))
# These are the option menus
dd_owner = StringVar(window)
w = OptionMenu(window, dd_owner, *owner, command=update_button)
w.grid(row=0, column=1)
#These are the buttons
store_button =Button(window, text="Store data!",command=store)
store_button.grid(column=0, row=31)
run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
def checker():
if data:
store_button.configure(state='normal')
else:
store_button.configure(state='disabled')
checker()
mainloop()
Upvotes: 1
Views: 366
Reputation: 47163
The two buttons should be disabled initially.
Then if owner is selected, enable "Run" button.
If "Run" button is clicked, enable "Store" button.
Below is a modified example:
import os
from tkinter import *
owner = ['Spain', 'United Kingdom', 'Malaysia']
path_spain = r"c:\data\FF\Desktop\PythonFolder\spain_software.py"
path_uk = r"c:\data\FF\Desktop\PythonFolder\uk_software.py"
path_malaysia = r"c:\data\FF\Desktop\PythonFolder\malaysia_software.py"
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
data = []
#Confirmation function
def store():
owner = dd_owner.get()
if owner and owner not in data:
data.append(owner)
print(data)
def run():
if dd_owner.get() == "Spain":
print("spain")
# os.system('python path_spain')
elif dd_owner.get() == "United Kingdom":
os.system('python path_uk')
elif dd_owner.get() == "Malaysia":
os.system('python path_malaysia')
# enable store button
store_button.config(state="normal")
def update_button(_):
# update text and enable the button
run_button.config(text="Run application {}".format(dd_owner.get()), state="normal")
# These are the option menus
dd_owner = StringVar(window)
w = OptionMenu(window, dd_owner, *owner, command=update_button)
w.grid(row=0, column=1)
#These are the buttons
store_button =Button(window, text="Store data!", command=store, state="disabled") # initially disabled
store_button.grid(column=0, row=31)
run_button = Button(window, text="Run application {}".format(dd_owner.get()),
bg="blue", fg="white",command=run, state="disabled") # initially disabled
run_button.grid(column=0, row=2)
# These are the titles
l1 = Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()
Upvotes: 2
Reputation: 3011
What do you mean by if data:
Data is a list. You can't use Data as a boolean in the if statement.
Upvotes: 0
Reputation: 3011
Use -
Button(root,state= DISABLED)
This will disable.
then -
Button(root,state= NORMAL)
This will enable the button
Don't use '' or "". Just write in CAPITAL
Upvotes: 0