Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

checkbox - Python tkinter checkbutton value always equal to 0

I put the checkbutton on the text widget, but everytime I select a checkbutton, the function checkbutton_value is called, and it returns 0.

Part of the code is :

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Each root window (and all of its children, and all related StringVars etc.) start a new, independent tcl interpreter. Widgets and variables associated with this window can't be used in another tcl interpreter. In your case, the StringVar 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...