[英]Python Tkinter, can't get value of checkbutton as child
I created some widgets in loop.我在循环中创建了一些小部件。 And i need to get values of em all.
我需要得到所有的价值。 I coded:
我编码:
from tkinter import *
class App():
def __init__(self):
self.ws = Tk()
self.frame = LabelFrame(self.ws)
self.frame.grid(row=1,column=1)
for i in range(16):
e = Label(self.frame, text=str(i + 1) + '.')
e.grid(row=i+1, column=1)
e1 = Entry(self.frame, width=8)
e1.grid(row=i+1, column=2)
e2 = Entry(self.frame)
e2.grid(row=i+1, column=3)
check = Checkbutton(self.frame, variable=BooleanVar(), onvalue=True, offvalue=False)
check.grid(row=i+1, column=4, sticky=E)
but = Button(self.ws,text='Get ALL',command=self.getall)
but.grid(row=17,column=1)
self.ws.mainloop()
def getall(self):
list = []
self.frame.update()
print('Child List:',self.frame.winfo_children())
for wid in self.frame.winfo_children():
if isinstance(wid,Entry):
list.append(wid.get())
elif isinstance(wid,Checkbutton):
self.frame.getvar(wid['variable'])
print('List:',list)
if __name__ == '__main__':
App()
It returns:它返回:
return self.tk.getvar(name) .TclError: can't read "PY_VAR0": no such variable
If i click all checkbuttons, it doesn't error but returns empty strings..Whats wrong here?如果我单击所有复选按钮,它不会出错,但会返回空字符串..这里有什么问题?
TKinter couldn't retrieve the variables of your checkboxes, because you have created these variables on the fly inside __init__()
scope, hence these variables are living in __init__()
call stack only, and once __init__()
finished its work, the garbage collector cleans these variables, so they are not reachable any more since they are not live in your main stack. TKinter 无法检索复选框的变量,因为您已经在
__init__()
范围内动态创建了这些变量,因此这些变量仅存在于__init__()
调用堆栈中,一旦__init__()
完成其工作,垃圾收集器清除这些变量,因此它们不再可访问,因为它们不在您的主堆栈中。
So, you need to keep them living in your main program stack.因此,您需要将它们保留在您的主程序堆栈中。 I have edited your code by adding a long-lived
dict()
for storing these checkboxes variables to be able to access them later.我通过添加一个长期存在的
dict()
来编辑你的代码来存储这些复选框变量,以便以后能够访问它们。
from tkinter import *
class App():
def __init__(self):
self.ws = Tk()
self.frame = LabelFrame(self.ws)
self.frame.grid(row=1, column=1)
self.checkboxesValues = dict()
for i in range(16):
self.checkboxesValues[i] = BooleanVar()
self.checkboxesValues[i].set(False)
e = Label(self.frame, text=str(i + 1) + '.')
e.grid(row=i+1, column=1)
e1 = Entry(self.frame, width=8)
e1.grid(row=i+1, column=2)
e2 = Entry(self.frame)
e2.grid(row=i+1, column=3)
check = Checkbutton(self.frame, variable=self.checkboxesValues[i])
check.grid(row=i+1, column=4, sticky=E)
but = Button(self.ws, text='Get ALL', command=self.getall)
but.grid(row=17, column=1)
self.ws.mainloop()
def getall(self):
list = []
self.frame.update()
print('Child List:', self.frame.winfo_children())
for wid in self.frame.winfo_children():
if isinstance(wid, Entry):
list.append(wid.get())
elif isinstance(wid, Checkbutton):
list.append(self.frame.getvar(wid['variable']))
print('List:', list)
if __name__ == '__main__':
App()
Now, checkboxesValues
variable lives in your App
's object stack, so your checkboxes variables are existing in your memory as long as your object is not destroyed.现在,
checkboxesValues
变量存在于您的App
的对象堆栈中,因此只要您的对象未被销毁,您的 checkboxes 变量就存在于您的内存中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.