Python tkinter checkbutton value always equal to 0
<p>I put the <code>checkbutton</code> on the <code>text</code> widget, but everytime I select a <code>checkbutton</code>, the function <code>checkbutton_value</code> is called, and it returns 0.</p>

<p>Part of the code is :</p>

<pre><code>def callback():

    file_name=askopenfilename()
    column_1rowname,column_name=draw_column(file_name)

    root = Tk()
    root.resizable(width=False,height=False)
    root.wm_title("Column")


    S = Scrollbar(root,orient="vertical")
    text=Text(root,width=15,height=10,yscrollcommand=S.set)
    S.config(command=text.yview)
    S.pack(side="right",fill="y")
    text.pack(side="left",fill="both",expand=True)

    #check the value of the checkbutton
    def checkbutton_value():

        if(var.get()):

            print 1
        else:

            print 0

    var=BooleanVar()
    chk = Checkbutton(root, text=column_1rowname[1], variable=var, command=checkbutton_value)
    text.window_create("end", window=chk)
    text.config(state=DISABLED)


errmsg='Error!'
Button(text='File Open',command=callback).pack(fill=X)


mainloop()
</code></pre>

<p>The problem is that you have more than one root window. You should only ever create exactly one instance of <code>Tk</code>, and call <code>mainloop</code> exactly once. If you need additional windows, create instances of <code>Toplevel</code>.</p>

<p>Each root window (and all of its children, and all related <code>StringVar</code>s etc.) start a new, <em>independent</em> tcl interpreter. Widgets and variables associated with this window can't be used in another tcl interpreter. In your case, the <code>StringVar</code> is associated with the first root window, but the widget is associated with the second. You can't share data between root windows like that.</p>

python checkbox tkinter