Search code examples
pythonclasstkinterttk

ttk checkbutton appears when loaded up with black box in it


taken from: tkk checkbutton appears when loaded up with black box in it

the provided solution works well with one checkbutton

import tkinter as Tk
from tkinter import IntVar
from tkinter.ttk import Frame, Checkbutton
class TestGui(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.var1 = IntVar()
        self.var1.set(1)
        button = Checkbutton(parent,
            text="Pick me, pick me!",
            variable=self.var1)        # note difference here
        button.grid()

root = Tk.Tk()
app = TestGui(root)
root.mainloop()

but when I create multiple buttons using a loop in my class, this only works for the last button.


Solution

  • cant really explain why, but I figured it out.

    doesnt work(using list):

        varss = []
        for n in range (2):
            date = datetime.today() + timedelta(days=n)
            day = datetime.strftime(date, "%d.%m.%y")
            for h in range(24):
    
                date = date.replace(hour=h,minute=0)
                txt_date = datetime.strftime(date,"%H:%M")
                var = IntVar()
                c = ttk.Checkbutton(tour_frame,text = txt_date,variable = var)
                var.set(1)
                c.grid(column = n , row = h + 1)
                varss.append(var)
    

    does work(using list of dicts):

        l= []
        for n in range (2):
            date = datetime.today() + timedelta(days=n)
            day = datetime.strftime(date, "%d.%m.%y")
            for h in range(24):
    
                date = date.replace(hour=h,minute=0)
                txt_date = datetime.strftime(date,"%H:%M")
                var = IntVar()
                c = ttk.Checkbutton(tour_frame,text = txt_date,variable = var)
                var.set(1)
                c.grid(column = n , row = h + 1)
                l.append(dict(datetime = date.replace(hour = h,minute=0,second=False,microsecond=False),value = var ))