Nathan McCoy Nathan McCoy - 2 months ago 13
Python Question

Retrieve Tkinter Checkbutton Status : TclError: can't read "PY_VAR": no such variable

I am using

Tkinter
in a basic Python 2.7 GUI application and I would like to retrieve the
Checkbutton
widget status (checked/unchecked) by using a
IntVar
but I am getting the following error.

TclError: can't read "PY_VAR": no such variable


I have followed the example on effbot about the Checkbutton Widget and I am using a different
IntVar
for each button and using a
callback
function to print the variable by calling the
getvar
function on the buttons.

My only goal is to view a status of the
Checkbutton
widget.

I am using the Tkinter Grid Geometry manger to place the widgets on the GUI. Here is a MCVE example that produces the error.

#!/usr/bin/env python

import Tkinter as tk

class Frame(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.buttons = dict() #number to button widget
self.createWidgets(master)

def printvar(self, button_number):
print self.buttons[button_number].getvar()

def createWidgets(self,master):
for n in range(0,4):
var = tk.IntVar()
button = tk.Checkbutton(
master,
variable=var,
command=lambda bn=n: self.printvar(bn)
)
button.grid(row=0, column=n)
self.buttons[n] = button

window = Frame(tk.Tk())
window.mainloop()


The code makes four numbered buttons passing the number to a
lambda
function which looks up the button in a
dict
and calls its corresponding
getvar
function.

If you run the example code it will produce the following error when you check any of the four buttons. It is in a file named
tktest.py


Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
return self.func(*args)
File "./tktest.py", line 20, in <lambda>
command=lambda bn=n: self.printvar(bn)
File "./tktest.py", line 12, in printvar
print self.buttons[button_number].getvar()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 513, in getvar
return self.tk.getvar(name)
TclError: can't read "PY_VAR": no such variable


There is no
var
variable for each button and I am therefore calling the
getvar
method.

Any alternative methodology for checking each individual
Checkbutton
widget status would also be acceptable.

Answer

EDIT: First create n number of var for use and than asign for each button and save in a list.

def createWidgets(self,master):
    self.vars=[]
    for n in range(0,4):
        var = tk.IntVar()
        self.vars.append(var)

    for n in range(0,4):
        button = tk.Checkbutton(
            master,
            variable=self.vars[n],
            command=lambda bn=n: self.printvar(bn)
        )
        button.grid(row=0, column=n)
        self.buttons[n] = button

and then you can change your method to this for call the var for the number

def printvar(self, button_number):
    print "The button:{} is {}".format(button_number,self.vars[button_number].get())

The output for example is:

The button:2 is 0
The button:2 is 1

when 0 is unchecked and 1 checked