Python-3 Tkinter checkbuttons in different classes activated together

65 Views Asked by At

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.

1

There are 1 best solutions below

0
mathengineer On

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()