0

I'm making a part of my program that contains checkboxes. There are a total of 8 checkboxes and the user can set a min and max of 3 of the boxes. This would be simple enough to determine how many are ticked by setting the on-value as 1, and add the total on-values among all the checkboxes, and if its not equal to 3, it'll say so.

However, I store specific information within these checkboxes, and I .get() these and add them to a list which I then put into a treeview and mysql database.

One way I thought I could go around this is by setting the offvalue to 1, and adding all the offvalues, and if the total offvalue is = 5, that means 5 boxes are not checked, hence 3 have been checked. However, I do not know a way to get both the offvalue and onvalue from a check box.

Any help would be appreciated.

    var1 = IntVar()
    musiccheck = Checkbutton(frame2, text='Music', variable = var1, onvalue='3', offvalue='1')
    musiccheck.pack()

    var2 = IntVar()
    ecocheck = Checkbutton(frame2, text='Econ', variable = var2, onvalue='5', offvalue='1')
    ecocheck.pack()

1 Answers1

0

It's a bit of cheap method but I was able to find a way to first determine if 3 checkboxes are selected, then if it passes, it proceeds to put them in a list, which I would then put into mysql db and treeview.

The code first checks if the box is selected by .get the checkbox then seeing if its equal to the onvalue, or in other words if its selected. If it does, it gets appended into a temporary list.

It proceeds to check all and append all the other checkboxes. Now I if len the list, if there is more or less than 3, it notifies the user, and if it has 3 values in the list, it passes.

    def check():

        var_len = []

        print("--")
        if var1.get() == 1:
            var_len.append(1)
        if var2.get() == 2:
            var_len.append(2)
        if var3.get() == 3:
            var_len.append(3)
        if var4.get() == 4:
            var_len.append(4)
        if var5.get() == 5:
            var_len.append(4)
            
        if len(var_len) == 3:
            print("good")
        else:
            print("choose 3 items")

        print("--")

    var1 = IntVar()
    musiccheck = Checkbutton(frame2, text='Music', variable = var1, onvalue='1', offvalue='0')
    musiccheck.pack()

    var2 = IntVar()
    ecocheck = Checkbutton(frame2, text='Econ', variable = var2, onvalue='2', offvalue='0')
    ecocheck.pack()

    var3 = IntVar()
    DGTcheck = Checkbutton(frame2, text='DGT', variable = var3, onvalue='3', offvalue='0')
    DGTcheck.pack()

    var4 = IntVar()
    chemcheck = Checkbutton(frame2, text='Chem', variable = var4, onvalue='4', offvalue='0')
    chemcheck.pack()

    var5 = IntVar()
    phycheck = Checkbutton(frame2, text='Phys', variable = var5, onvalue='5', offvalue='0')
    phycheck.pack()