Unknownzdx Unknownzdx - 1 year ago 62
Python Question

Enabling or disabling checkbox with conditionals

In my code i'm trying to disable the hi box when no is checked and enable it when yes is checked. I attempt to do this in the changed function. However when running the code and checking the yes box hi is still fine and when checking the yes only the yes box i get:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python35-32\lib\idlelib\run.py", line 119, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python35-32\lib\queue.py", line 172, in get
raise Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Ahmed\AppData\Local\Programs\Python\Python35-32\lib\tkinter__init__.py", line 1550, in call
return self.func(*args)
File "C:\Users\Ahmed\Desktop\no_yes.py", line 13, in changed
NameError: name 'ENABLE' is not defined

It might be me using idle and not importing the right thing (i have nothing modified or installed with idle just plain comes from python site) but Im thinking its the way i attempt to enable or disable the checkboxes in the changed function. Basically I want to know what to do to enable and disable the checkboxes upon meeting the proper requirements(disable if no selected, enable if only yes selected.)

#Import tkinter to make gui
from tkinter import *
from tkinter import ttk

def changed(*args):
value = b1.get()
value2 = b2.get()
value3 = b3.get()
print (b3.get()=="1")
if b1.get()==1:
elif b2.get() == "1" and (b1.get() == "1") == False:
elif b3.get() != "1":
elif b3.get() == "1":
#Sets title and creates gui
root = Tk()

#Configures column and row settings and sets padding
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

b1 = StringVar()
c1 = ttk.Checkbutton(mainframe, text='No',
command=changed, variable=b1)
c1.grid(column=1, row=1, sticky=(W, E))

b2 = StringVar()
c2 = ttk.Checkbutton(mainframe, text='Yes',
command=changed, variable=b2)
c2.grid(column=2, row=1, sticky=(W, E))

b3 = StringVar()
c3 = ttk.Checkbutton(mainframe, text='Hi',
command=changed, variable=b3)
c3.grid(column=2, row=2, sticky=(W, E))

result = StringVar()
ttk.Label(mainframe, textvariable=result).grid(column=1, row=2, sticky=(W))

Answer Source

Alright, so disabling and enabling goes like so:

button.state(['disabled'])            # set the disabled flag, disabling the button
button.state(['!disabled'])           # clear the disabled flag
button.instate(['disabled'])          # return true if the button is disabled, else false
button.instate(['!disabled'])         # return true if the button is not disabled, else false
button.instate(['!disabled'], cmd)    # execute 'cmd' if the button is not disabled
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download