Python Forum
Int Variables in different Tkinter windows only returning 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Int Variables in different Tkinter windows only returning 0
#1
I have checkboxes in a secondary tkinter window (not root) that only return an IntVar of 0 even when selected. This is not a problem in the main window.

I have the code working in the main root window and when selected, the checkboxes return 1 and not 0, however when using the exact same code in a separate tkinter window it returns only 0's. I have added window2.mainloop() at the end which did nothing.

button_list = []              #create list to hold checkbox values
def done_press():           #what to do when done button pressed
    for x in range(len(data)):
        print(button_list[x].get())
play = Tk()                #second window called play
row=[1,2,3,4,5,6,7,8,9]                             
Label(play, text='Whats on').grid(row=0, column=0, padx=30, pady=20, columnspan=3)
for box in range(len(data)):    #create checkboxes
    button_list.append(IntVar())   #append checkbox list for each selected 
    button = Checkbutton(play, text=data[box], variable=button_list,)  
    button.grid(column=0,row=row[box], pady=5, padx=30)
done = Button(play, text='Done', command=done_press)
done.grid(column=0,row=11, columnspan=2)
play.mainloop()
any help would be appreciated, thanks.
Reply
#2
The code shown does not run, there is no tkinter import and data has not been defined.
Unable to compare if you are using the exact same code in main root window as secondary window as only one windows code is shown.
In the code shown when the Checkbutton's are created instead of each individual IntVar being passed as an argument to parameter variable, the whole button_list is being passed.
Reply
#3
(May-26-2019, 09:37 AM)Yoriz Wrote: The code shown does not run, there is no tkinter import and data has not been defined.
Unable to compare if you are using the exact same code in main root window as secondary window as only one windows code is shown.
In the code shown when the Checkbutton's are created instead of each individual IntVar being passed as an argument to parameter variable, the whole data list is being passed.

Sorry, i tried to keep the snippet of code as short as possible so it was easier to understand, here is code that could be copy and pasted into IDLE:
from tkinter import *

data = ['a','b','c','d','e','f','g','h','i']

button_list = []              #create list to hold checkbox values
def done_press():           #what to do when done button pressed
    for x in range(len(data)):
        print(button_list[x].get())
play = Tk()                #second window called play
row=[1,2,3,4,5,6,7,8,9]                             
Label(play, text='Whats on').grid(row=0, column=0, padx=30, pady=20, columnspan=3)
for box in range(len(data)):    #create checkboxes
    button_list.append(IntVar())   #append checkbox list for each selected 
    button = Checkbutton(play, text=data[box], variable=button_list,)  
    button.grid(column=0,row=row[box], pady=5, padx=30)
done = Button(play, text='Done', command=done_press)
done.grid(column=0,row=11, columnspan=2)
play.mainloop()
I now see that this doesnt even work when the window is the main window... do you know how i would make it so that each individual IntVar is passed as an argument to parameter variable?
Reply
#4
Please see the following thread
https://python-forum.io/Thread-Namespace...th-imports

Consider learning classes for GUI code
https://python-forum.io/Thread-Classes-Class-Basics

The code below has comments on how to fix and improve the code.
# from tkinter import * # don't flood the namespace with * imports
import tkinter as tk  # add tk. infront of following tkinter objects

checkbox_texts = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
]  # give variables a more descriptive name

checkbox_variables = []


def done_press():
    # for x in range(len(data)): # don't use the index to iterate the items
    #     print(button_list[x].get())
    for checkbox in checkbox_variables:  # iterating the items directly
        print(checkbox.get())


play = tk.Tk()
# row=[1,2,3,4,5,6,7,8,9] # not required can use enumerate on list items
tk.Label(play, text="Whats on").grid(row=0, column=0, padx=30, pady=20, columnspan=3)
# for box in range(len(data)): # don't use the index to iterate the items
for row, text in enumerate(
    checkbox_texts, 1
):  # iterate actual item and use enumerate to get row value
    checkbox_variable = tk.IntVar()  # create the varibale
    button = tk.Checkbutton(
        play, text=text, variable=checkbox_variable
    )  # use text & checkbox_variable directly
    checkbox_variables.append(checkbox_variable)  # append checkbox_variable
    button.grid(column=0, row=row, pady=5, padx=30)  # row from enumerate
btn_done = tk.Button(play, text="Done", command=done_press)
btn_done.grid(column=0, row=11, columnspan=2)
play.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter two windows instead of one jacksfrustration 7 780 Feb-08-2024, 06:18 PM
Last Post: deanhystad
  pass a variable between tkinter and toplevel windows janeik 10 2,142 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Tkinter multiple windows in the same window tomro91 1 791 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Dual Tkinter windows and shells Astrikor 6 3,843 Sep-03-2020, 10:09 PM
Last Post: Astrikor
  [Tkinter] How to compare two variables correctly in tkinter scratchmyhead 2 3,819 May-10-2020, 08:04 PM
Last Post: scratchmyhead
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 1,703 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Returning a value from a tkinter.button call markr0804 4 25,009 Feb-16-2020, 10:35 AM
Last Post: markr0804
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,364 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Tkinter bringing variables from 1 frame to another zukochew 6 12,421 Dec-26-2019, 05:27 AM
Last Post: skm
  How to close one of the windows in Tkinter scratchmyhead 3 4,762 Dec-21-2019, 06:48 PM
Last Post: pashaliski

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020