EXIT_FAILURE
EXIT_FAILURE

Reputation: 288

Use dictionary with checkbuttons (boxes update all at once and values in dictionary aren't updated)

I'm trying to create a GUI with tkinter that includes several checkboxes. The variables associated with those checkboxes are supposed to be stored in a common dictionary (see the example below). The problem with my code so far is that all the checkboxes update at the same time when only one of them is clicked and that the values in the dictionary aren't updated.

import tkinter as tk
config = {"option0": False, "option1": False}

root = tk.Tk()

def print_values():
    print(config["option0"])
    print(config["option1"])

chk_option0 = tk.Checkbutton(master=root, text='option0', variable=config["option0"], onvalue=True, offvalue=False)
chk_option0.grid(row=0, column=0, sticky = "W")

chk_option1 = tk.Checkbutton(master=root, text='option1', variable=config["option1"], onvalue=True, offvalue=False)
chk_option1.grid(row=1, column=0, sticky = "W")

btn_Print = tk.Button(master=root, text="Print values", command=lambda : print_values())
btn_Print.grid(row=2, column=0, sticky = "W")

root.mainloop()

Upvotes: 0

Views: 162

Answers (2)

rupinderg00
rupinderg00

Reputation: 189

You will have to initialise the variables with BooleanVar() if you want to store the output of checkboxes. ( Similarly, IntVar() and StringVar() also exists if you set the off value and the on value with int and string respectively). And finally, you will have to use .get() function to get the value stored in it.

import tkinter as tk

config = {"option0": False, "option1": False}

root = tk.Tk()

def print_values():
    print(config["option0"].get())
    print(config["option1"].get())

config["option0"] = tk.BooleanVar()
config["option1"] = tk.BooleanVar()
chk_option0 = tk.Checkbutton(master=root, text='option0', variable=config["option0"], onvalue=True, offvalue=False)
chk_option0.grid(row=0, column=0, sticky = "W")

chk_option1 = tk.Checkbutton(master=root, text='option1', variable=config["option1"], onvalue=True, offvalue=False)
chk_option1.grid(row=1, column=0, sticky = "W")

btn_Print = tk.Button(master=root, text="Print values", command=lambda : print_values())
btn_Print.grid(row=2, column=0, sticky = "W")

root.mainloop()

Upvotes: 1

acw1668
acw1668

Reputation: 46688

You should use BooleanVar() instead of False for values of config dictionary:

import tkinter as tk

root = tk.Tk()

config = {"option0": tk.BooleanVar(), "option1": tk.BooleanVar()}

def print_values():
    print(config["option0"].get())
    print(config["option1"].get())

chk_option0 = tk.Checkbutton(master=root, text='option0', variable=config["option0"], onvalue=True, offvalue=False)
chk_option0.grid(row=0, column=0, sticky = "W")

chk_option1 = tk.Checkbutton(master=root, text='option1', variable=config["option1"], onvalue=True, offvalue=False)
chk_option1.grid(row=1, column=0, sticky = "W")

btn_Print = tk.Button(master=root, text="Print values", command=lambda : print_values())
btn_Print.grid(row=2, column=0, sticky = "W")

root.mainloop()

Upvotes: 1

Related Questions