Home > OS >  How to get the values in array
How to get the values in array

Time:04-28

I am trying to return the values in the array vars but I am only getting this back. I am quite new so please can someone explain what these values are ad why they aren't returning 1 or 0 for the state of the checkbutton?

[<tkinter.IntVar object at 0x0318BCD0>, <tkinter.IntVar object at 0x0318BD70>, <tkinter.IntVar 
object at 0x0318BDD0>, <tkinter.IntVar object at 0x0318BE30>, <tkinter.IntVar object at 0x0318BE90>, <tkinter.IntVar object at 0x0318BF10>, <tkinter.IntVar object at 0x0318BF70>, <tkinter.IntVar object at 0x0318BFD0>, <tkinter.IntVar object at 0x03196050>, <tkinter.IntVar object at 0x03196070>, <tkinter.IntVar object at 0x031960D0>, <tkinter.IntVar object at 0x03196110>, <tkinter.IntVar object at 0x03196150>, <tkinter.IntVar object at 0x03196190>, <tkinter.IntVar object 
at 0x031961D0>, <tkinter.IntVar object at 0x03196210>, <tkinter.IntVar object at 0x03196250>, <tkinter.IntVar object at 0x03196290>, <tkinter.IntVar object at 0x031962D0>, <tkinter.IntVar object at 0x03196310>, <tkinter.IntVar object at 0x03196350>, <tkinter.IntVar object at 0x03196390>, <tkinter.IntVar object at 0x031963D0>, <tkinter.IntVar object at 0x03196410>, <tkinter.IntVar object at 0x03196450>, <tkinter.IntVar object at 0x03196490>, <tkinter.IntVar object at 0x031964D0>, <tkinter.IntVar object at 0x03196510>, <tkinter.IntVar object at 0x03196550>, <tkinter.IntVar object at 0x03196590>, <tkinter.IntVar object at 0x031965D0>, <tkinter.IntVar object at 0x03196610>, <tkinter.IntVar object at 0x03196650>, <tkinter.IntVar object at 0x03196690>, <tkinter.IntVar object at 0x031966D0>, <tkinter.IntVar object at 0x03196710>, <tkinter.IntVar object at 0x03196750>, <tkinter.IntVar object at 0x03196790>, <tkinter.IntVar object at 0x031967D0>, <tkinter.IntVar object at 0x03196810>, <tkinter.IntVar object at 0x03196850>, <tkinter.IntVar object at 0x03196890>, <tkinter.IntVar object at 0x031968D0>, <tkinter.IntVar object at 0x03196910>, <tkinter.IntVar object at 0x03196950>, <tkinter.IntVar object at 0x03196990>, <tkinter.IntVar object at 0x031969D0>, <tkinter.IntVar object at 0x03196A10>, <tkinter.IntVar object at 0x03196A50>, <tkinter.IntVar object at 0x03196A90>]

This is the code that I am using from this post

import tkinter as tk

root = tk.Tk()

scrollbar = tk.Scrollbar(root)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

checklist = tk.Text(root, width=20)
checklist.pack()

vars = []
for i in range(50):
    var = tk.IntVar()
    vars.append(var)
    checkbutton = tk.Checkbutton(checklist, text=i, variable=var)
    checklist.window_create("end", window=checkbutton)
    checklist.insert("end", "\n")

checklist.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=checklist.yview)

# disable the widget so users can't insert text into it
checklist.configure(state="disabled")

root.mainloop()

CodePudding user response:

To get the value out of a tk.IntVar() you need to use the .get() method. So if you want to get a list of values you need to use .get() on every element of the vars array.

And vars is a saved keyword so you should use it, you could use the name tk_vars instead.

values = []

for var in tk_vars:
   values.append(var.get())

print(values)

And if you want to use a list comprehension:

values = [var.get() for var in tk_vars]

CodePudding user response:

You didn't use the .get() method on var. Read more about these methods here.

  • Related