I am trying to change the state of a button based on the user input of two other widgets contained in my program. Ideally I would like the button to change state any time one/both of the widgets values is missing and only be enabled when both values are present. I'm unsure of how to go about doing this. I have tried writing a function to check the value of the two widgets control variables and configure the button accordingly, but with no luck. If any other information is needed for clarity/context, please let me know. Thank you in advance.
import tkinter as tk
from tkinter import ttk
results = dict() # holds control vars
categories = ['a', 'b', 'c'] # options for combobox
def change_state():
c = results['category'].get()
n = results['number_of_recipes'].get()
if c and n != 0:
button.configure(state=tk.NORMAL)
else:
button.configure(state=tk.DISABLED)
root = tk.Tk()
results['category'] = tk.StringVar()
ttk.Combobox(
root,
textvariable=results['category'],
values=categories,
justify='right'
).grid(
column=0,
row=0
)
results['number_of_recipes'] = tk.IntVar()
ttk.Spinbox(
root,
textvariable=results['number_of_recipes'],
from_=1, to=20, increment=1,
justify='right',
).grid(
column=1,
row=0
)
button = ttk.Button(
root,
text='Button',
state=tk.DISABLED
)
change_state()
button.grid(
column=2,
row=0
)
root.mainloop()