フレーム内でジオメトリを個別に管理する例を得たいと思っていました。次のコードを書いて、旗のように見える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でコードを確認できたら嬉しいです。
私は、TwoFramesクラスで0から始まるので0とすることを意味していたので、行番号を渡すのは大丈夫だと仮定しました。私はあなたが同じものだと言っているのと同じ仮定をしました。しかし、それは明らかに異なって機能し、どういうわけか彼らは同一ではないということを意味します。私はそれら(2つのグリッドオプションが同じであると言っている)がどう違うのだろうか? – Nae
@Nae:最初の値はデフォルトで0ですが、2番目の値は1にデフォルト設定されています。両方のグリッド呼び出しが0の行を使用していると想定していますが、 'grid'を呼び出すたびに、明示的に行を設定しないと行がインクリメントされます。 –
ああ、私はあなたのコードの違いに気付かなかった。ごめんなさい。しかし、幾何学は同様に書かれていますが、今回は列番号が渡されていないので、TwoLabelsのクラス関数はどのようにして元のようになりましたか? – Nae