1

I am trying to create a function inside a class and with this function, I want to calculate how many checkboxes clicked.

#This function assesses number of clicked items in my class's list
    def badper(self):
        window=Tk()
        window.config(bg='lightgreen')
        # total = 0
        def show(self):
            new_label = Label(window, text=var.get()).pack()
        var=IntVar()
        def create_check_button(self,i):
            var = IntVar()
            c=Checkbutton(window,text=self.badcharacter[i],variable=var)
            c.pack()
            # if c == 1:
            #     global total
            #     total += 1
        #This loop creates checkboxes in the equivalent of number items of my list
        for i in range(6):
            create_check_button(self,i)

        #This button should show how many items clicked so far
        my_button=Button(window,text="show", font=("Arial", 12, "bold"),command=show(self)).pack()

Another related question is the IntVar() does not show 0,1 when I write the code below:

def badper(self):
    window=Tk()
    window.config(bg='lightgreen')
    def show():
        my_label=Label(window,text=var.get()).pack()
    var=IntVar()

    c = Checkbutton(window, text="abc", variable=var)
    c.deselect()
    c.pack()
    my_button = Button(window, text="show", font=("Arial", 12, "bold"), command=show).pack()     
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ugur
  • 33
  • 9
  • store the reference to each checkbutton create in a list/dict of variables. You can then access them and check if they are _checked_ or not – FrainBr33z3 Sep 30 '21 at 10:47
  • 1
    You need to store those `IntVar` instead because `Checkbutton` does not have function to get its check state. – acw1668 Sep 30 '21 at 10:52
  • I tried to sum but in some way, I cannot sum checked boxes. And the moderator associated my question with another question but my question is not about creating multiple check buttons. I am looking for an answer to how can I calculate them by using them within a function? – ugur Oct 01 '21 at 13:39

0 Answers0