【问题标题】:AttributeError: 'NoneType' object has no attribute 'children' in __init__.py destroyAttributeError:'NoneType' 对象在 __init__.py 中没有属性 'children' 销毁
【发布时间】:2018-05-12 06:49:55
【问题描述】:

我刚开始使用 tkinter,偶然发现了一个我自己无法解决的问题:
我想自己写一个小部件,我称之为口袋。这实际上是我的窗口的一部分,可以滑动打开或隐藏。

class pocket(tk.Frame):
    def __init__(self, parent, master=None, expand_Bool=True):
        tk.Frame.__init__(self, parent)
        self.master = master
        self.Content = tk.Frame(self)        
        self.Button_on = tk.Button(self,text='Open',command=lambda: self.expand())
        self.Button_hide = tk.Button(self,text='Hide',command=lambda: self.hide())
        self.Content.grid(row=1,column=0)
        self.Button_on.grid(row=0,column=0)
        self.Button_hide.grid(row=0,column=0)
        if expand_Bool:
            self.expand()
        else:
            self.hide()

    def expand(self):
        self.Button_on.grid_remove()
        self.Button_hide.grid()
        self.Content.grid()
    def hide(self):
        self.Button_on.grid()
        self.Button_hide.grid_remove()
        self.Content.grid_remove()
    def get_contentframe(self):
        return self.Content



if __name__=='__main__':
    root=tk.Tk()

    pocket1 =pocket(root)
    pocket1.grid(row=0,column=0)
    label1=tk.Label(pocket1.get_contentframe(),text='Text')
    label1.grid(row=1,column=1)
    root.mainloop()

“小部件”本身运行良好。但是当我尝试关闭窗口时,我收到一条错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\...\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\...\tkinter\__init__.py", line 2055, in destroy
    for c in list(self.children.values()): c.destroy()
  File "C:\...\tkinter\__init__.py", line 2300, in destroy
    if self._name in self.master.children:
AttributeError: 'NoneType' object has no attribute 'children'

我怎样才能避免这个错误?还是有更好的方法来做到这一点?
提前致谢。
问候

【问题讨论】:

  • 你使用tkinter.after()还是threads?您可能必须在关闭程序之前停止它们。
  • BWT:代替command=lambda: self.expand() 你可以使用command=self.expand(没有lambda()
  • 好吧,self.master 确实是None,那么你为什么期望它有一个名为children 的属性?

标签: python tkinter parent children attributeerror


【解决方案1】:

问题是

self.master = master

坦率地说,当你执行时

tk.Frame.__init__(self, parent)

然后它会自动创建变量self.master 并分配parent

self.master = parent

你不必这样做。


在你的代码中masterNone 所以最后你得到了

self.master = None

因此它失去了对parent 的访问权限,并且在尝试关闭孩子时出现问题。


完整代码

import tkinter as tk

class Pocket(tk.Frame):

    def __init__(self, master=None, expand_bool=True):
        tk.Frame.__init__(self, master)

        self.content = tk.Frame(self)        
        self.button_on = tk.Button(self, text='Open', command=self.expand)
        self.button_hide = tk.Button(self, text='Hide', command=self.hide)
        self.content.grid(row=1, column=0)
        self.button_on.grid(row=0, column=0)
        self.button_hide.grid(row=0, column=0)

        if expand_bool:
            self.expand()
        else:
            self.hide()

    def expand(self):
        self.button_on.grid_remove()
        self.button_hide.grid()
        self.content.grid()

    def hide(self):
        self.button_on.grid()
        self.button_hide.grid_remove()
        self.content.grid_remove()

    def get_contentframe(self):
        return self.content

if __name__=='__main__':
    root = tk.Tk()

    pocket1 = Pocket(root)
    pocket1.grid(row=0, column=0)

    label1 = tk.Label(pocket1.get_contentframe(), text='Text')
    label1.grid(row=1, column=1)

    root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    相关资源
    最近更新 更多