Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

Tkinter Example: use many Checkbuttons with IntVar or StringVars [GB]

It uses dictionary to keep many IntVar which are used with Checkbutton

import tkinter as tk

# --- functions ---

def confirm():
    for key, item in all_int_vars.items():
        if item.get() == 1:
            print(key)

# --- main ---

items = ['pizza', 'breadsticks', 'wings', 'CocaCola', 'brownie'] 

root = tk.Tk()

all_int_vars = {item:tk.IntVar() for item in items}

for item in items:
    cb = tk.Checkbutton(root, text=item, variable=all_int_vars[item], anchor='w', onvalue=1, offvalue=0, width=15)
    cb.pack()

button = tk.Button(root, text="confirm", command=confirm)
button.pack()

root.mainloop()

It uses list to keep many StringVar which are used with Checkbutton

import tkinter as tk

# --- functions ---

def confirm():
    for string_var in all_string_vars:
        text = string_var.get()
        if text:
            print(text)

# --- main ---

items = ['pizza', 'breadsticks', 'wings', 'CocaCola', 'brownie'] 

root = tk.Tk()

all_string_vars = []

for item in items:
    string_var = tk.StringVar()
    cb = tk.Checkbutton(root, text=item, variable=string_var, anchor='w', onvalue=item, offvalue='', width=15)
    cb.pack()
    all_string_vars.append(string_var)

button = tk.Button(root, text="Confirm", command=confirm)
button.pack()

root.mainloop()
Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury