0

It happened with checkbuttons in different clases. It can be seen by running this code and clicking in any checkbutton:

from tkinter import *
import constant

class Frame1(Frame):
        def __init__(self, parent):
            Frame.__init__(self, parent, bg="lightgray")
            self.parent = parent
            self.texto=[None]*10
            self.cboton=[None]*10
            self.widgets()
        def widgets(self):
            for row1 in range(10):
                t="V%s"%row1
                self.cboton[row1]=Checkbutton(self, padx=7, relief=RIDGE, text=t,                                    
                                onvalue=1, offvalue=0,  font='Courier 10')
                self.cboton[row1].grid(row=row1, column=0)
                
                self.texto[row1] = Text(self,width=5, height=1)
                self.texto[row1].insert(INSERT, "00.00")
                self.texto[row1].grid(row=row1, column=1)
                
          

class MainW(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.mainWidgets()
    def mainWidgets(self):
        self.window=[None]*18
        for col in range(18):
            texto="Bloq-#%i"%col
            self.label = Label(self, text=texto)
            self.label.grid(row=0, column=col)
            self.window[col] = Frame1(self)
            self.window[col].grid(row=1, column=col)

if __name__=="__main__":
    app = MainW(None)
    app.mainloop()

I expected that a var of a class is hidden for other classes.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
mathengineer
  • 140
  • 6
  • 1
    Checkbuttons require a `variable=` option to specify some Tk Var object that is to hold their state - would need to be an `IntVar` in this case, since your `onvalue=`/`offvalue=` options are integers. In the absence of `variable=`, they're all defaulting to the same variable name, so their states are all tied together. – jasonharper Nov 10 '22 at 01:30

1 Answers1

0

I MUST add a variable to store the checkbutton data:

Before:

self.cboton[row1]=Checkbutton(self, padx=7, relief=RIDGE,text=t,                          onvalue=1, offvalue=0)

After:

self.CheckVar[row1]=IntVar()
self.cboton[row1]=Checkbutton(self, padx=7, relief=RIDGE, variable = self.CheckVar[row1], text=t,                                    
                            onvalue=1, offvalue=0,  font='Courier 10')

The complete running correct code is here:

from tkinter import *
import constant

class Frame1(Frame):
        def __init__(self):
            Frame.__init__(self)
            self.texto=[None]*10
            self.cboton=[None]*10
            self.CheckVar=[None]*10
            self.widgets()
        def widgets(self):
            for row1 in range(10):
                t="V%s"%row1
                self.CheckVar[row1]=IntVar()
                self.cboton[row1]=Checkbutton(self, padx=7, relief=RIDGE, variable = self.CheckVar[row1], text=t,                                    
                                onvalue=1, offvalue=0,  font='Courier 10')
                self.cboton[row1].grid(row=row1, column=0)
                
                self.texto[row1] = Text(self,width=5, height=1)
                self.texto[row1].insert(INSERT, "00.00")
                self.texto[row1].grid(row=row1, column=1)
                
          

class MainW(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.mainWidgets()
    def mainWidgets(self):
        self.window=[None]*18
        for col in range(18):
            texto="Bloq-#%i"%col
            self.label = Label(self, text=texto)
            self.label.grid(row=0, column=col)
            self.window[col] = Frame1()
            self.window[col].grid(row=1, column=col)

if __name__=="__main__":
    app = MainW()
    app.mainloop()
mathengineer
  • 140
  • 6