mercredi 17 novembre 2021

get value of tkinter checkboxes [duplicate]

I cannot get the value of checkboxes to be returned by the get_states function below. After checking several items, and clicking "Run", they all print a state of 0 (unchecked) no matter what I do. The name and number of the checkboxes changes on every run, so I need them to be generated dynamically. I've searched every stack overflow question on tkinter checkboxes and tried different solutions but I get the same result no matter how I rewrite this.

class CheckBoxes(tk.Frame):
def __init__(self, parent, columnheaders):
    tk.Frame.__init__(self, parent)
    self.btn_quit = tk.Button(parent, text='Quit', font="arial 20",
                              command=self.quit)
    self.btn_quit.pack()
    self.btn_run = tk.Button(parent, text='Run', font='arial 20',
                             command=self.get_states)
    self.btn_run.pack()
    self.vars = []
    # Helper variable
    r = 0
    for header in columnheaders:
        # Variable and checkbox for the title.
        var = tk.IntVar()
        chk = tk.Checkbutton(parent, text=header, variable=var)
        chk.pack()
        self.vars.append(var)
        r += 1

def get_states(self):
    for item in self.vars:
        print(item)
        print(item.get())



Aucun commentaire:

Enregistrer un commentaire