-2

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.

cl_cookie
  • 1
  • 2
  • 3
    So you've shown us code that works, and not the code that actually exhibits the problem - seriously??? I'm going to take a wild guess here, and say that you probably aren't saving all of the individual `IntVar`s that are attached to the buttons (in a list, perhaps). – jasonharper Jan 11 '22 at 16:36
  • 1
    Have you tried using `var1` instead of `self.var1`? Also, the point of providing your code is to _demonstrate your problem_. If you only provide code that works, it makes it harder for us to help you. Please provide a [example] that _demonstrates the problem_. See [ask] and the [help] for more information on asking good questions. – Sylvester Kruin Jan 11 '22 at 18:22
  • im sorry jason , didn't mean to offend your honour. I'm new here, I'll improve. thank u for the nice information guys ! – cl_cookie Jan 11 '22 at 21:41

1 Answers1

0

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 ))
cl_cookie
  • 1
  • 2