简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'children' in __init__.py destroy

I just started out on tkinter and stumbeled over a problem I could not solve by my own:
I wanted to write a widget myself, that I call pocket. Which effectivly is part in my window that can be slid open or hidden.

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

The "widget" itself works quiet fine. but when I try to close the window I get an error message:

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'

How can I evoid that error ? or is there a even better way to do this?
Thanks in advance.
Greetings

Problem is

self.master = master

Frankly, when you execute

tk.Frame.__init__(self, parent)

then it automatically creates variable self.master and assigns parent .

self.master = parent

You don't have to do it.


In your code master is None so finally you get

self.master = None

so it loose access to parent and it has problem when it tries to close children.


Full code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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