0

I've made an array of checkboxes in my tkinter application which gets appended to, whenever a plot is made, in order to hide/show each of them. However, the plot hides just fine, but never shows when the checkbox is clicked again. I'm assuming this is because the variable is refusing to update:

        self.vararray.append(tk.IntVar(value=0))
        cntr = self.plot_counter
        c = tk.Checkbutton(self.checkbox_frame,selectcolor="#F194EA", text=tail[:-4], variable=self.vararray[-1],command= lambda var=cntr: self.hide_plot(filename, visible = self.vararray[cntr]))
        c.pack(side=tk.LEFT)
        c.var = self.vararray[-1]
        self.btnarray.append(c)
        self.plot_counter+=1

I have gone through other answers and I've even made sure to append the variables and checkboxes in an array, but to no avail. The relevant code for hiding the plot is here:

def hide_plot(self, filename, visible):
    if(visible.get() == 0):
        for i in range(0, len(self.plotarray)):
            if self.filearray[i] == filename:
                for j in range(0, len(self.plotarray[i])):
                    self.plotarray[i][j].set_visible(False)
        #visible.set(1)
    else: 
        for i in range(0, len(self.plotarray)):
            if self.filearray[i] == filename:
                for j in range(0, len(self.plotarray[i])):
                    self.plotarray[i][j].set_visible(True)
        #visible.set(0)
    self.fig.canvas.draw()

I have to manually set the variable inside of the function in order for it to start working, what am I doing wrong here? Thanks!

AetherPrior
  • 105
  • 6
  • 1
    You are making a very common mistake. See https://stackoverflow.com/q/17677649/7432 – Bryan Oakley Jun 19 '20 at 20:03
  • @BryanOakley I don't see what I'm not doing that's in the answers over there, I also changed the cntr in the lambda function to var, but to no avail – AetherPrior Jun 20 '20 at 08:44
  • `command= lambda var=cntr: self.hide_plot(filename, visible = self.vararray[cntr]` binds `cntr` to the current value. But what about `filename`? Is that not intended to be different for each checkbutton? – Karl Knechtel Aug 19 '22 at 02:43

0 Answers0