0

I'm stuck on the following problem. With a for-loop, I want to make a few checkboxes that automatically update a label stating whether the checkbox is ticked or not. However, it gives the wrong results (it always says that the checkboxes are ticked, whether this is the case or not; noteworthy is the fact that the checkboxes are unticked by default), see here how the GUI looks like (including error). The IntVars corresponding with the checkboxes are working correctly, as can be seen when ticking at least one of the checkboxes and pressing a button whose function is to read the checkboxes. See also the following code:

import tkinter as tk

top = tk.Tk()
n_passes = 3

checkbox_var = [0] * n_passes
checkbox = [0] * n_passes

def tick_passes(i): # update label saying if checkboxes are ticked
    if checkbox_var[i].get == 0:
        label = tk.Label(top, text = f"pass #{i} not ticked")
    else:
        label = tk.Label(top, text = f"pass #{i} ticked")
    label.grid(row = 1, column = i)

def check_checkbox_var(): # check whether checkbox_var[i] is updated
    for i in range(n_passes):
        print(f"checkbox_var[i].get() = {checkbox_var[i].get()}")

for i in range(n_passes):
    checkbox_var[i] = tk.IntVar() # turn on/off certain passes
    print(f"checkbox_var[i].get() = {checkbox_var[i].get()}")
    checkbox[i] = tk.Checkbutton(top, text = f"Tick pass {i}", variable =
    checkbox_var[i], command = tick_passes(i))
    checkbox[i].grid(row = 0, column = i, sticky=tk.W)

    var_button = tk.Button(top, text = "Check checkbox_var", command =
    check_checkbox_var).grid(row = 2, column = 0) # check whether checkbox_var[i] is updated

top.mainloop()

Could somebody help me with updating the labels? If there is another way to fix this issue, e.g. with buttons to be pressed instead of checkbuttons to be ticked, that would also work for mee.

Chris
  • 1
  • 1
  • 1
    `if checkbox_var[i].get == 0:` should be `if checkbox_var[i].get() == 0:`, and `command = tick_passes(i)` should be `command=lambda i=i:tick_passes(i)` – Jason Yang Oct 11 '21 at 13:06
  • Move statement for button out of your last loop; create labels first, then update it in `tick_passes` – Jason Yang Oct 11 '21 at 13:17
  • Thank you so much @JasonYang! You helped me a lot – Chris Oct 11 '21 at 13:18

1 Answers1

-1

i is always 2 because you're actually not running any loop after mainloop is started. The following kind of works but you need to change something about the labels, because right now all labels are just added on top of each other. You should create them once and then just update the text but I'll leave that part for you.

import tkinter as tk

top = tk.Tk()
n_passes = 3

checkbox_var = [0] * n_passes
checkbox = [0] * n_passes

def tick_passes(): # update label saying if checkboxes are ticked
    for i in range(n_passes):
        if checkbox_var[i].get() == 0:
            label = tk.Label(top, text = f"pass #{i} not ticked")
        else:
            label = tk.Label(top, text = f"pass #{i} ticked")
        label.grid(row = 1, column = i)

def check_checkbox_var(): # check whether checkbox_var[i] is updated
    for i in range(n_passes):
        print(f"checkbox_var[i].get() = {checkbox_var[i].get()}")

for i in range(n_passes):
    print(i)
    checkbox_var[i] = tk.IntVar() # turn on/off certain passes
    print(f"checkbox_var[i].get() = {checkbox_var[i].get()}")
    checkbox[i] = tk.Checkbutton(top, text = f"Tick pass {i}", variable =
    checkbox_var[i], command = tick_passes)
    checkbox[i].grid(row = 0, column = i, sticky=tk.W)

    var_button = tk.Button(top, text = "Check checkbox_var", command =
    check_checkbox_var).grid(row = 2, column = 0) # check whether checkbox_var[i] is updated

top.mainloop()
helpneeded
  • 117
  • 9
  • This creates an entirely new label each time `tick_passes` is called. That's not a good solution. – Bryan Oakley Oct 11 '21 at 16:20
  • @Bryan Oakley That's what I wrote on top of it... But I'm pretty sure the person who asked can handle that problem himself and I don't need to solve it for him. I solved the problem he asked for. – helpneeded Oct 12 '21 at 05:44
  • 1
    Yes, this is quite useful. The label is no problem. Thanks a lot! – Chris Oct 12 '21 at 14:42