2017-09-29 6 views
1

ボタンを含む別のフレームの上にあるフレームの内側にツリービューがあります。私は、ウィンドウのサイズを変更しても、ボタンフレームが同じことをするのを止めない限り、上のフレームを展開したいと思います。 Pythonの2.7.5でTkinter/ttkを使って別のウィジェットを垂直に展開する

コード:

class MyWindow(Tk.Toplevel, object): 
     def __init__(self, master=None, other_stuff=None): 
     super(MyWindow, self).__init__(master) 
     self.other_stuff = other_stuff 
     self.master = master 
     self.resizable(True, True) 
     self.grid_columnconfigure(0, weight=1) 
     self.grid_rowconfigure(0, weight=1) 

     # Top Frame 
     top_frame = ttk.Frame(self) 
     top_frame.grid(row=0, column=0, sticky=Tk.NSEW) 
     top_frame.grid_columnconfigure(0, weight=1) 
     top_frame.grid_rowconfigure(0, weight=1) 
     top_frame.grid_rowconfigure(1, weight=1) 

     # Treeview 
     self.tree = ttk.Treeview(top_frame, columns=('Value')) 
     self.tree.grid(row=0, column=0, sticky=Tk.NSEW) 
     self.tree.column("Value", width=100, anchor=Tk.CENTER) 
     self.tree.heading("#0", text="Name") 
     self.tree.heading("Value", text="Value") 

     # Button Frame 
     button_frame = ttk.Frame(self) 
     button_frame.grid(row=1, column=0, sticky=Tk.NSEW) 
     button_frame.grid_columnconfigure(0, weight=1) 
     button_frame.grid_rowconfigure(0, weight=1) 

     # Send Button 
     send_button = ttk.Button(button_frame, text="Send", 
     command=self.on_send) 
     send_button.grid(row=1, column=0, sticky=Tk.SW) 
     send_button.grid_columnconfigure(0, weight=1) 

     # Close Button 
     close_button = ttk.Button(button_frame, text="Close", 
     command=self.on_close) 
     close_button.grid(row=1, column=0, sticky=Tk.SE) 
     close_button.grid_columnconfigure(0, weight=1) 

私は他の場所でこのようなインスタンスを作る:

window = MyWindow(master=self, other_stuff=self._other_stuff) 

私が試してみました何: はボタンのみが消え作られたサイズを変更できるロックしようとしました。私も体重を変更しようとしましたが、私の現在の設定だけがすべてが画面上に現れる唯一の方法です。事前に enter image description here

感謝を:私がないようにしたいどのような When it first launches

:それは常にどのくらいの高さに関係なく、どのように見えるか

答えて

3

問題は、ボタンフレームが成長しているということではなく、上部フレームが成長しているが、すべてのスペースを使用していないということです。これは、行1にtop_frameという重みを与えているにもかかわらず、行1に何も入れていないためです。余分なスペースは、その重みのため行1に割り当てられていますが、行1は空です。

これを簡単に視覚化するには、top_frameをtk(ttkではなく)のフレームに変更し、一時的にそれに特有の背景色を付けます。ウィンドウのサイズを変更すると、top_frameはウィンドウ全体を塗りつぶしますが、ウィンドウは部分的に空であることがわかります。

はこのようなtop_frame作成:ウィンドウのサイズを変更する場合

top_frame = Tk.Frame(self, background="pink") 

を...以下の画像のような画面が得られます。ピンクtop_frameが表示されており、button_frameが好ましいサイズのままであることに注意してください。

screenshot showing colored empty space

あなたは、単にコードのこの1行を削除することによってこの問題を解決することができます:あなたは任意のより良いことを説明していませんでした

top_frame.grid_rowconfigure(1, weight=1) 
+1

。私はグリッドの今より良い理解と、それがどのように動作するのか、ありがとう! – vaponteblizzard

関連する問題