tkinter - 添加小部件时如何停止改变框架的大小?

tkinter - How to stop frame changing size when widget is added?

在页面框架中,我在 innerFrame 中对两个框架进行了加权,这样它们每个都占据了屏幕的一半,但是,当我向其中一个框架添加一个小部件时,我使用了一个列表框作为例如,由于它很大,其中一个框架现在比另一个占据更多。我如何做到这一点,以便在添加小部件时框架不会改变大小,并且每个框架的大小保持 window?

的一半

这是我的代码:

import tkinter as tk
from tkinter import ttk

class Program(tk.Tk):        
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default = "")
        tk.Tk.wm_title(self, "")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Page, Other):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(Page)

    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class Page(tk.Frame):

    def __init__(self, parent, controller):


        tk.Frame.__init__(self, parent)

        innerFrame = tk.Frame(self, bg="red")
        innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0)


        innerFrame.grid_rowconfigure(0, weight=1)
        innerFrame.grid_rowconfigure(1, weight=1)
        innerFrame.grid_columnconfigure(0, weight=1)

        #First Half
        frameOne = tk.Frame(innerFrame, bg="pink")
        frameOne.grid(row=0, sticky="NSWE")

        #Second Half
        frameTwo = tk.Frame(innerFrame, bg="green")
        frameTwo.grid(row=1, sticky="NSWE")

        lb = tk.Listbox(frameTwo)
        lb.pack(fill="both", expand=True)


class Other(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)



app = Program()
app.state('zoomed')
app.mainloop()

Grid 接受参数uniform,可以取任意值。具有相同值的所有行(或所有列)都被视为 "uniform group" 的一部分。这会强制行(或列)的大小与其权重成比例。

如果您在一个框架中有两行,两行具有相同的权重,并且都属于同一个统一组,那么它们将分别占据可用空间的 50% space。

在你的情况下你可以这样做:

innerFrame.grid_rowconfigure(0, weight=1, uniform="x")
innerFrame.grid_rowconfigure(1, weight=1, uniform="x")

(同样,"x" 是任意的;它可以是任何值,只要两行的值相同即可)

tk 的官方文档(构建 tkinter 的基础)是这样描述的(参见 http://tcl.tk/man/tcl8.5/TkCmd/grid.htm#M24

The -uniform option, when a non-empty value is supplied, places the row in a uniform group with other rows that have the same value for -uniform. The space for rows belonging to a uniform group is allocated so that their sizes are always in strict proportion to their -weight values.

...

When multiple rows or columns belong to a uniform group, the space allocated to them is always in proportion to their weights. (A weight of zero is considered to be 1.) In other words, a row or column configured with -weight 1 -uniform a will have exactly the same size as any other row or column configured with -weight 1 -uniform a. A row or column configured with -weight 2 -uniform b will be exactly twice as large as one that is configured with -weight 1 -uniform b.


注意:uniform 选项没有出现在 tkinter 文档中,但它是一个完全有效的选项。多年来,它一直是 tk(tkinter 建立在其上)的一部分。

你可以用max-size,min-sixe来画框:

master = tk()
master.minsize(width=777, height=777)
master.maxsize(width=777, height=777)