问题描述
我将 checkbutton
放在 text
小部件上,但是每次我选择 checkbutton
时,函数 checkbutton_value
被调用,并返回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.
部分代码是:
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()
推荐答案
问题是您有多个根窗口.您应该只创建一个 Tk
的实例,并只调用一次 mainloop
.如果需要其他窗口,请创建顶级
的实例.
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
.
每个根窗口(及其所有子窗口以及所有相关的 StringVar
等)都将启动一个新的独立 tcl解释器.与此窗口关联的窗口小部件和变量不能在另一个tcl解释器中使用.在您的情况下, StringVar
与第一个根窗口相关联,而小部件与第二个根窗口相关联.您不能在这样的根窗口之间共享数据.
Each root window (and all of its children, and all related StringVar
s 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.
这篇关于Python Tkinter Checkbutton值始终等于0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!