Home > Software design >  (Python) trying to write a Function to reads a dataframe (from excel) and then loop same number of r
(Python) trying to write a Function to reads a dataframe (from excel) and then loop same number of r

Time:03-15

But I am having a hard time being able to figure out how to .get() from tkinter to call the corresponding value when a particular check box is clicked.

class overview:
 def __init__(self,m):
    self.m = m

    m.title("Title")
    m.geometry('800x600')
    m['bg'] = '#2874A6'
    
    self.frame= LabelFrame()
    self.frame.configure(width=210)
    self.frame.grid(row = 0,column = 0, sticky = NSEW)
    for x in pind1:
        a = pind1.index(x)
        l = Checkbutton(self.frame, text=names1[a], variable = "N" str(a), onvalue = 1, offvalue = 0)
        
        l.pack()

CodePudding user response:

The variable option requires an instance of one of tkinter's special variables (StringVar(), etc.). You're just passing in a string.

If you want to use a name like N1, N2, etc, the simplest solution is to use a dictionary to store the variables.

self.vars = {}
for x in pind1:
    ...
    var = StringVar()
    self.vars[f"N{a}"] = var
    l = Checkbutton(..., variable = var, ...)
    ...

You could also use a list if you don't need to name the variables with a leading "N".

  • Related