Retrieve Tkinter Checkbutton Status : TclError: can't read "PY_VAR": no such variable
<p>I am using <code>Tkinter</code> in a basic Python 2.7 GUI application and I would like to retrieve the <code>Checkbutton</code> widget status (checked/unchecked) by using a <code>IntVar</code> but I am getting the following error. </p>

<pre><code>TclError: can't read "PY_VAR": no such variable
</code></pre>

<p>I have followed the example on <a href="http://effbot.org/tkinterbook/checkbutton.htm" rel="nofollow">effbot about the Checkbutton Widget</a> and I am using a different <code>IntVar</code> for each button and using a <code>callback</code> function to print the variable by calling the <code>getvar</code> function on the buttons.</p>

<p>My only goal is to view a status of the <code>Checkbutton</code> widget. </p>

<p>I am using the Tkinter Grid Geometry manger to place the widgets on the GUI. Here is a <a href="http://stackoverflow.com/help/mcve">MCVE</a> example that produces the error.</p>

<pre><code>#!/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()
</code></pre>

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

<p>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 <code>tktest.py</code></p>

<pre><code>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 &lt;lambda&gt;
    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
</code></pre>

<p>There is no <code>var</code> variable for each button and I am therefore calling the  <code>getvar</code> method.</p>

<p>Any alternative methodology for checking each individual <code>Checkbutton</code> widget status would also be acceptable. </p>

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

<pre><code>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
</code></pre>

<p>and then you can change your method to this for call the var for the number</p>

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

<p>The output for example is:</p>

<pre><code>The button:2 is 0
The button:2 is 1
</code></pre>

<p>when 0 is unchecked and 1 checked</p>

python python-2.7 user-interface tkinter