2017-10-30 13 views
0

フレーム内でジオメトリを個別に管理する例を得たいと思っていました。次のコードを書いて、旗のように見えるGUIを作りました。なぜ行番号を渡す必要がありますか?

輸入TkinterのTK

# a class that has 2 columns of frames inside 
class TwoFrames(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 

     # creates 2 frame objects and passes self as parent, which 
     # means object created using TwoFrames class is the parent 
     self.frame1 = tk.Frame(self) 
     self.frame2 = tk.Frame(self) 

     #manages 2 frames geometry 
     self.frame1.grid(column=0, sticky="nsew") 
     self.frame2.grid(column=1, sticky="nsew") 

     # enables resizing for 0th row, and 1st and 2nd columns of an 
     # object of this class 
     tk.Grid.rowconfigure(self, 0, weight=1) 
     tk.Grid.columnconfigure(self, 0, weight=1) 
     tk.Grid.columnconfigure(self, 1, weight=1) 


class TwoLabels(tk.Frame): 
    def __init__(self, master=None, color=True): 
     super().__init__(master) 

     #creates 2 Label objects with TwoLabels object as parent 
     self.label1 = tk.Label(self) 
     self.label2 = tk.Label(self) 

     # configures the background color of labels for demonstrative 
     # purposes 
     if color: 
      #label1 will have red color 
      self.label1.configure(bg="black") 
      #label2 will have blue color 
      self.label2.configure(bg="white") 
     else: 
      #label1 will have blue color 
      self.label1.configure(bg="white") 
      #label2 will have red color 
      self.label2.configure(bg="black") 


     # manages the geometry 
     self.label1.grid(row=0, sticky="nsew") 
     self.label2.grid(row=1, sticky="nsew") 

     # enables resizing like above, but this time for 2 rows and 1 
     # column 
     tk.Grid.rowconfigure(self, 0, weight=1) 
     tk.Grid.rowconfigure(self, 1, weight=1) 
     tk.Grid.columnconfigure(self, 0, weight=1) 


# creates the mainWindow 
mainWindow = tk.Tk() 

# creates a mainFrame that has 2 frames in it 
mainFrame = TwoFrames(mainWindow) 

# manages geometry of mainFrame and display it 
mainFrame.pack(fill="both", expand=True) 

# creates row_labels1 and row_labels2, both has 2 colored labels inside 
row_labels1 = TwoLabels(mainFrame.frame1, True) 
row_labels2 = TwoLabels(mainFrame.frame2, False) 

# manages geometry of labels inside frames and displays them 
row_labels1.pack(fill="both", expand=True) 
row_labels2.pack(fill="both", expand=True) 

# run the application 
mainWindow.mainloop() 

しかし皮肉なことに、コードではなく、私がやろうとしていることは不可能であるかのように、2行目の1垂直半を持ってチェッカーフラッグを制作など。後で私は変更しました。

#manages 2 frames geometry 
self.frame1.grid(row=0, column=0, sticky="nsew") 
self.frame2.grid(row=0, column=1, sticky="nsew") 

#manages 2 frames geometry 
self.frame1.grid(column=0, sticky="nsew") 
self.frame2.grid(column=1, sticky="nsew") 

一部と私が最初に意図したとおりにそれが働きました。私はそれがうまく動作しますが、

  • 私は、ジオメトリは、少なくともクラス 的かに管理されているかはわかりません。それは...ですか?

  • とにかく私が渡したものと等しいと思われるものは、 行番号が変わりますか?

また、Code Reviewでコードを確認できたら嬉しいです。

答えて

1

ジオメトリが少なくともクラスベースで管理されているかどうかはわかりません。それは...ですか?

ジオメトリ管理は決してクラス単位で管理されません。ジオメトリ管理は、ウィジェット自体の親子構造に基づいています。あるクラスのウィジェットがそのクラスの子である場合(クラスがフレームの場合など)、その場合、管理はクラス単位で行われていると見なすことができます。 Tkinterはあなたのクラス構造を知らない、気にしません。ウィジェットの親子関係のみを気にします。

私はとにかく渡すものと等しいと思われるものは、行番号が変わりますか?

あなたの前提はfalseです。行を指定しない場合は、gridを呼び出すたびに1行ずつ自動インクリメントされます。

つまり

、この:

self.frame1.grid(column=0, sticky="nsew") 
self.frame2.grid(column=1, sticky="nsew") 

...(各行にrowの値に細心の注意を払って)これと機能的に同じです:

self.frame1.grid(row=0, column=0, sticky="nsew") 
self.frame2.grid(row=1, column=1, sticky="nsew") 

あなたドン場合がgridを複数回呼び出しても、列を指定するのではなく、デフォルトで0になります。言い換えれば:

self.label1.grid(row=0, sticky="nsew") 
self.label2.grid(row=1, sticky="nsew") 

(各行のcolumnに注意を払う)これに機能的に同一である:

経験則として
self.label1.grid(column=0, row=0, sticky="nsew") 
self.label2.grid(column=0, row=1, sticky="nsew") 

(および明確化のために)あなたが常に供給しなければなりません行番号と列番号の両方そうすることで、ウィジェットがどこに配置されているかの曖昧さがすべて除去されます。

+0

私は、TwoFramesクラスで0から始まるので0とすることを意味していたので、行番号を渡すのは大丈夫だと仮定しました。私はあなたが同じものだと言っているのと同じ仮定をしました。しかし、それは明らかに異なって機能し、どういうわけか彼らは同一ではないということを意味します。私はそれら(2つのグリッドオプションが同じであると言っている)がどう違うのだろうか? – Nae

+1

@Nae:最初の値はデフォルトで0ですが、2番目の値は1にデフォルト設定されています。両方のグリッド呼び出しが0の行を使用していると想定していますが、 'grid'を呼び出すたびに、明示的に行を設定しないと行がインクリメントされます。 –

+0

ああ、私はあなたのコードの違いに気付かなかった。ごめんなさい。しかし、幾何学は同様に書かれていますが、今回は列番号が渡されていないので、TwoLabelsのクラス関数はどのようにして元のようになりましたか? – Nae

関連する問題