2017-11-28 34 views
0

私は自分自身で解決できなかった問題をtkinterから取り除きました。
私は自分でウィジェットを作成したいと思っていました。私はポケットを呼ぶ。効果的に開いているか隠れていることができる私の窓の一部です。AttributeError: 'NoneType'オブジェクトに__init__.pyの 'children'属性がありません。

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' 

このエラーを回避するにはどうすればよいですか?またはこれを行うためのより良い方法がありますか?
ありがとうございます。
挨拶

+0

あなたは 'tkinter.after()'や 'threads'を使うのですか?プログラムを終了する前に、プログラムを停止する必要があるかもしれません。 – furas

+0

BWT: 'command = lambda:self.expand()' 'command = self.expand'(' lambda'と '()'を除く) – furas

+0

BTW:[PEP 8スタイルガイドfor Python Code ](https://www.python.org/dev/peps/pep-0008/) – furas

答えて

0

問題は、あなたが

tk.Frame.__init__(self, parent) 

を実行すると、それが自動的に変数self.masterを作成し、parentを割り当て、

率直に言って
self.master = master 

です。

self.master = parent 

あなたはこれを行う必要はありません。あなたのコードmaster


ので、最終的にはあなたがparentにそれ緩いアクセス

self.master = None 

を取得し、それが子供たちを閉じようとしたとき、それは問題を抱えているNoneです。


全コード

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() 
関連する問題