0

I am creating a GUI with checkbuttons and will use the state of each button to determine if another method is called later in my script.

I am going this via a loop, however when I disable or enable any checkbutton, it then controls the state of all the rest.

Here is the snippet of code:


        #List of checkbutton text
        self.source_list = ["a","b","c","d","e","f","g","h","i","j","k"]
        #The variable for each text button which will be used later in the script to determine which method to call e.g. if self.source_var[i].get() == True: do something
        self.source_var = []
        self.var = BooleanVar(value=1)
        j = 0
        k = 0

        #loop to create checkbttons
        for i in range(len(self.source_list)):
            self.source_var.append(self.var) #add var to list
            ttk.Checkbutton(self.window, text = self.source_list[i], var = self.source_var[i]).grid(row=k,column=j,rowspan=1,sticky=W)
            k += 1
            #new column
            if k == 4:
                k = 0
                j += 1

I am pretty new to python so I could be doing something very simple wrong but cannot find out what it is after research on the internet for the last hour.

Thanks in advance!

SOLUTION thanks to @jizhihaoSAMA


        #List of checkbutton text
        self.source_list = ["a","b","c","d","e","f","g","h","i","j","k"]
        # #The variable for each text button which will be used later in the script to determine which method to call e.g. if self.source_var[i].get() == True: do something
        self.source_var = []

        j = 0
        k = 0

        #loop to create checkbttons
        for i in range(len(self.source_list)):
            self.var = BooleanVar(value=1)
            self.source_var.append(self.var) #add var to list
            ttk.Checkbutton(self.window, text = self.source_list[i], var = self.source_var[i]).grid(row=k,column=j,rowspan=1,sticky=W)
            k += 1
            #new column
            if k == 4:
                k = 0
                j += 1


        #Dictionary for checking variable state later
        self.source_dict = dict(zip(self.source_list,self.source_var))

        if self.source_dict.get("a").get()) == True:
        #do XYZ
Tobias Funke
  • 1,614
  • 3
  • 13
  • 23
  • ***controls the state of all the rest.***: You are using the same `.append(self.var)` which groups all `Checkbutton`. You have to use a own `var` for every `Checkbutton`. Read up on [The Tkinter Checkbutton Widget](http://effbot.org/tkinterbook/checkbutton.htm) – stovfl Jun 06 '20 at 14:06
  • @stovfl no, this will technically work but I already had my python in this format and am trying to condense it to align with best practices. – Tobias Funke Jun 06 '20 at 14:15
  • ***no, this will technically work***: This is contradict to your questions title? What i'm missing? – stovfl Jun 06 '20 at 14:16
  • 1
    You could just put the `self.var = BooleanVar(value=1)` in the `for` loop. – jizhihaoSAMA Jun 06 '20 at 14:40
  • @jizhihaoSAMA thanks, I had tried that but I could not then reference these later with the get method for each individual checkbutton? Each checkbutton corresponds to a different logic. – Tobias Funke Jun 06 '20 at 14:42
  • 1
    Sure you could,you need to iterate the `self.source_var` and do `.get()`. – jizhihaoSAMA Jun 06 '20 at 14:44
  • See this [answer](https://stackoverflow.com/a/59889417/7414759) to @jizhihaoSAMA suggestion. – stovfl Jun 06 '20 at 14:50
  • @jizhihaoSAMA yes you are correct. I slightly misread your initial comment. If you add your comment below I will upvote and accept. Thanks! – Tobias Funke Jun 06 '20 at 14:51
  • 1
    I marked this as a duplicate, but I updated the duplicate to mention how to create the variables in a loop. – Bryan Oakley Jun 06 '20 at 14:53

0 Answers0