代码之家  ›  专栏  ›  技术社区  ›  vaponteblizzard

垂直扩展一个小部件,同时使用Tkinter/ttk锁定另一个小部件

  •  2
  • vaponteblizzard  · 技术社区  · 8 年前

    我在一个框架内有一个树状视图,它位于另一个包含按钮的框架顶部。我想顶部框架扩大时,我调整窗口,但保持按钮框架做同样的事情。

    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

    提前谢谢。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Bryan Oakley    8 年前

    问题不是按钮框架在增长,而是顶部框架在增长,但没有使用所有的空间。这是因为您正在给 top_frame 权重为1,但不在第1行中放置任何内容。由于行1的权重,正在为其分配额外的空间,但行1为空。

    顶部框架 并暂时给它一个独特的背景色。当你调整窗口大小时,你会看到, 顶部框架

    这样地:

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

    顶部框架 正在展示,而且 button_frame 保持其首选尺寸。

    screenshot showing colored empty space

    top_frame.grid_rowconfigure(1, weight=1)
    
    推荐文章