2017-12-12 13 views
0

のエントリを複製します。Tkinterの - 私は複数のモードを持つことになりますと、モードの変化に応じて、ウィンドウのさまざまな部分にフレームを変更したいTkinterのプログラムを書いていた辞書と予想外のパック()の挙動

次のコードが含まれるフレームを切り替えることができ抽象SwitcherClassを作成することを意図しています。 SwitchedFrameはSwitcherClassから派生し、切り替えるべきフレーム(この場合はMyclassとMyotherclass)をリストするclass属性を持っています。

それは2つの予期しない動作があります。SwitcherClassで

  1. 辞書self.childrenは、2つのエントリを持つ必要があり、それは4で終了。 SubClassオブジェクトはキーと値のように辞書内で終了するので、ループを実行するたびに2つの辞書エントリが作成されます。

  2. パック()とpack_forget()は何もしないSwitcherClassに呼び出します。実際、SwitcherClassの子がパックされていないと、メインウィンドウにはまだ表示されます。どうして?

これらのパズルのいずれかまたは両方に対する回答をいただければ幸いです。まず

import tkinter as tk 

class Myclass(tk.Frame): 
    def __init__(self,parent): 
     super().__init__(parent) 
     tk.Button(text='Myclass',command=parent.switch).pack() 

class Myotherclass(tk.Frame): 
    def __init__(self,parent): 
     super().__init__(parent) 
     tk.Button(text='Myotherclass',command=parent.switch).pack() 

class SwitcherClass(tk.Frame): 
    def __init__(self,parent): 
     super().__init__(parent) 
     self.children = {} 
     for label,Subclass in self.contains: 
      self.children[label] = Subclass(self) 
     print(self.children) 
     self.frame = 'first' 
     self.children[self.frame].pack()   

    def switch(self): 
     old_frame = self.frame 
     if self.frame == 'second': 
      self.frame = 'first' 
     else: 
      self.frame = 'second' 
     self.children[old_frame].pack_forget() 
     self.children[self.frame].pack() 

class SwitchedFrame(SwitcherClass): 
    contains = (('first',Myclass),('second',Myotherclass)) 


root = tk.Tk() 
tk.Label(root,text='root frame').pack() 
SwitchedFrame(root).pack() 
root.mainloop() 
+1

'tk.Button()'を 'parent'なしで使うと' root'に割り当てられます。 – furas

+0

'tkiner'はすでに' self.children'を使って自分の子供を保持しています - 別の名前を使用します。 'self.my_children' – furas

答えて

1

: - すなわちtkinter内のすべてのウィジェットは、そう、あなたのために別の名前を使用して自分の子供を保つためにself.childrenを使用しています。 self.my_children。今すぐ自分自身のウィジェットとウィジェットがtkinterによって自動的に追加されるのを見てください。

第二:親のないウィジェットがこのようにあなたのボタンがあまりにもrootに割り当てられているrootに割り当てられ、そしてあなたがいないサブフレームでは、メインウィンドウにすべての時間を参照してください(ただし、彼らは内部には大きさを持たないボタンのないフレームを変更)されます。

あなたは、フレームを修正するためのボタンを割り当てることButton(self, ...)を使用する必要があります。

import tkinter as tk 

class Myclass(tk.Frame): 
    def __init__(self, parent): 
     super().__init__(parent) 
     tk.Button(self, text='Myclass',command=parent.switch).pack() 

class Myotherclass(tk.Frame): 
    def __init__(self,parent): 
     super().__init__(parent) 
     tk.Button(self, text='Myotherclass',command=parent.switch).pack() 

class SwitcherClass(tk.Frame): 
    def __init__(self, parent): 
     super().__init__(parent) 
     self.my_children = {} 
     for label,Subclass in self.contains: 
      self.my_children[label] = Subclass(self) 
     for x in self.my_children: 
      print('child:', x) 
     self.frame = 'first' 
     self.my_children[self.frame].pack()   

    def switch(self): 
     old_frame = self.frame 
     if self.frame == 'second': 
      self.frame = 'first' 
     else: 
      self.frame = 'second' 
     self.my_children[old_frame].pack_forget() 
     self.my_children[self.frame].pack() 

class SwitchedFrame(SwitcherClass): 
    contains = (('first',Myclass), ('second',Myotherclass)) 


root = tk.Tk() 
tk.Label(root,text='root frame').pack() 
SwitchedFrame(root).pack() 
root.mainloop() 
+0

大きな説明。ありがとう。 –

関連する問題