繁体   English   中英

Python Tkinter,无法获得作为孩子的checkbutton的价值

[英]Python Tkinter, can't get value of checkbutton as child

我在循环中创建了一些小部件。 我需要得到所有的价值。 我编码:

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()

它返回:

 return self.tk.getvar(name) .TclError: can't read "PY_VAR0": no such variable

如果我单击所有复选按钮,它不会出错,但会返回空字符串..这里有什么问题?

TKinter 无法检索复选框的变量,因为您已经在__init__()范围内动态创建了这些变量,因此这些变量仅存在于__init__()调用堆栈中,一旦__init__()完成其工作,垃圾收集器清除这些变量,因此它们不再可访问,因为它们不在您的主堆栈中。

因此,您需要将它们保留在您的主程序堆栈中。 我通过添加一个长期存在的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()

现在, checkboxesValues变量存在于您的App的对象堆栈中,因此只要您的对象未被销毁,您的 checkboxes 变量就存在于您的内存中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM