jeudi 31 octobre 2019

How to check the state (enabled/disabled) of a checkbox in python (tkinter)

I have a program I am writing in Python and I'm having trouble with checking the state of a checkbox. I do NOT want to check whether it is checked or unchecked, but rather if it is disabled or enabled.

As a background, I have numerous checkboxes which i want displayed on screen, but based on the users selection within an option menu i want certain checkboxes to enable or disable. Ok easy enough. Now to the problem. I also have two buttons radiobuttons called "select ALL" and "select NONE"

My issue is that when i choose select ALL or select NONE it enables or disables ALL checkboxes, even those which are disabled.

I don't want to post the whole code because it is quite long so I will post a snippet of each section for reference, but would really appreciate any help in this. (NOTE: I hardcoded the state of the checkbuttons in the code below for testing/ease, but they will be enabled/disabled through a command from the option menu as mentioned above. I also only included two checkbuttons so that the code is not so long)

I have done a lot of searching but can only find info on how to enable/disable a checkbutton and how to check whether it is checked or not but not how to check whether or not it is disabled.

def select_all():
        var1.set(1), var2.set(1), var3.set(1), var4.set(1), var5.set(1), var6.set(1), var7.set(1), var8.set(1), var9.set(
        1), var10.set(1)


def select_none():

    var1.set(0), var2.set(0), var3.set(0), var4.set(0), var5.set(0), var6.set(0), var7.set(0), var8.set(0), var9.set(
        0), var10.set(0)
r = tk.Tk()
r.title('Title')

checkFrame = Frame(r)
checkFrame.pack()

var1 = IntVar()
C1 = Checkbutton(checkFrame, text='Option 1', state='disable', variable=var1)
C1.grid(row=0, column=0, sticky=W, pady=4, padx=15)
var2 = IntVar()
C2 = Checkbutton(checkFrame, text='Option 2', state='normal', variable=var2)
C2.grid(row=0, column=1, sticky=E, pady=4, padx=15)

selectAllCheck = IntVar()
Radiobutton(selectAllFrame, text='Select ALL', indicatoron=0, width=15, variable=selectAllCheck, value=1,
            command=select_all).grid(row=0, column=0, sticky=W, pady=10)
Radiobutton(selectAllFrame, text='Select NONE', indicatoron=0, width=15, variable=selectAllCheck, value=2,
            command=select_none).grid(row=0, column=1, sticky=E, pady=10)



Aucun commentaire:

Enregistrer un commentaire