使Tkinter Frame Widget高度为80%


console_frame = LabelFrame(root)
console_frame.pack(side="top", fill="x")

这里有一个框架,叫做console_frame,里面有一个文本框。我想让这个框架的高度是屏幕的80%,另一个框架的高度是屏幕的20%,但我也希望这是动态工作的,也就是小部件继续调整窗口的大小。

我尝试将框架的高度设置为屏幕高度的20%,但它不是动态的,因为小部件在调整大小后只是保持相同的高度。

当您使用网格放置方法时,您可以设置在调整窗口大小时每行展开或收缩的份额。这可能就是你要找的:

from tkinter import *

root = Tk()
root.geometry("800x500")
root.rowconfigure(0, weight=1) ## Weighs 1
root.rowconfigure(1, weight=4) ## Weights 4 time as much as the first row, thereby filling 4/(1+4)=80%
top_frame = Frame(root, bg="red",width=800)
top_frame.grid(row=0, column=0, sticky="NSEW")
bottom_frame = Frame(root, bg="blue",width=800)
bottom_frame.grid(row=1, column=0, sticky="NSEW")
root.mainloop()

最新更新