垂直展开一个控件,同时用Tkinter/ttk锁定另一个控件

垂直展开一个控件,同时用Tkinter/ttk锁定另一个控件

问题描述:

我在坐在另一个包含按钮的框架顶部的框架内有一个树形视图。当我调整窗口大小时,我希望顶部框架展开,但保持按钮框架不变。垂直展开一个控件,同时用Tkinter/ttk锁定另一个控件

代码在Python 2.7.5:

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) 

我曾尝试: 试图锁定可调整大小,只发按钮消失。我也尝试过改变权重,但我目前的配置是屏幕上显示的所有内容的唯一方式。

它应该永远是什么样子,不管多久高度: When it first launches

我想什么来防止:提前 enter image description here

感谢。

问题不在于按钮框架在不断增长,而在于顶部框架正在增加,但并未使用所有空间。这是因为您给予top_frame的第1行的权重为1,但不要将任何内容放入第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

您可以通过简单地去除这一行代码解决这个问题:

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

你不能有任何更好的解释。现在我对网格有了更好的理解,以及它如何工作,谢谢! – vaponteblizzard