如何在 tkinter 中显式调整帧大小?

car*_*tta 4 python tkinter python-3.x

所以我目前正在编写一段代码,稍后将集成到其他东西中以充当设置配置器。目前,我想要一个像下面这样布局的窗口: 布局

其中每个彩色框都是一个框架。此窗口不可调整大小,并且始终为 480x720 像素。因此,我希望我使用的 3 个框架、sideBar(黄色)、容器(蓝色)和静态(红色)始终保持相同的大小并以大致相同的比例填充如上图所示的窗口(不需要是精确的)。

此窗口的代码如下

        self.window = tk.Tk()

        self.windowHeight = 480
        self.windowLength = 720
        self.windowDimensions = str(self.windowLength)+"x"+str(self.windowHeight) #make diemnsions string; dimensions are set as a single string
        self.window.geometry(self.windowDimensions)
        self.window.resizable(width=False, height=False)

        self.container = tk.Frame(self.window, relief="sunken", borderwidth=2) #instantiate new window
        self.sideBar = tk.Frame(self.window,  relief="sunken", borderwidth=2)
        self.static = tk.Frame(self.window, relief="sunken", borderwidth=2)

        self.sideBar.grid_propagate(False)
        self.sideBar.grid(row=0, column=0)
        self.container.grid(row=0,column=1)
        self.static.grid(row=5, column=1)


        self.configuratorObject = configuratorObject 

        audioButton = tk.Button(self.sideBar, text="Audio Page", command=lambda: self.raisePage("audioPage"))
        colourButton = tk.Button(self.sideBar, text="Colours", command=lambda: self.raisePage("coloursPage"))
        saveButton = tk.Button(self.static, text = "Save", state="disabled")
        applyButton = tk.Button(self.static, text = "Apply", state="disabled")
        audioButton.pack()
        colourButton.pack()
        saveButton.pack()
        applyButton.pack()
Run Code Online (Sandbox Code Playgroud)

我试图更改网格的heightwidth参数,但它们似乎真的没有做任何事情。那么我该如何明确定义框架的布局和大小呢?

任何帮助表示赞赏

Bry*_*ley 6

In the comments you wrote

If theres a way of getting tkinter to do it then that'd be great

That is definitely the preferred way, over forcing widgets to be a particular size.

We'll start by using pack instead of grid for the three frames. For such a basic layout it requires fewer lines of code than grid.

self.sideBar.pack(side="left", fill="y")
self.static.pack(side="bottom", fill="x")
self.container.pack(side="top", fill="both", expand=True)
Run Code Online (Sandbox Code Playgroud)

Next, add the buttons on the left. This will cause the left frame to shrink in width to fit the buttons. Because we used fill="y", the height will be forced to remain the full height of the window.

audioButton.pack(side="top", fill="x")
colourButton.pack(side="top", fill="x")
Run Code Online (Sandbox Code Playgroud)

Finally, add the buttons on the bottom. Your original code shows them stacked top-to-bottom but your illustration shows them in a single horizontal row. This example adheres to the illustration.

applyButton.pack(side="right", padx=10)
saveButton.pack(side="right", padx=10)
Run Code Online (Sandbox Code Playgroud)

With that we end up with a window that looks like the following, and the proportions and orientation stays exactly the same when you resize the window:

截屏


Note: you can do this with grid too, but it requires a few more lines of code to apply weights to the rows and columns. I personally prefer pack when the layout doesn't naturally fit in a grid since it requires fewer lines of code.