因此,我正在创建一个 GUI,并且我正在尝试制作它,以便所有内容都能适当地显示在屏幕上。我已经绘制了我希望 GUI 的每个部分看起来像它的大小的粗略草图,所以我知道所有东西的粗略尺寸。
然而,我遇到的第一个问题是设置屏幕的左半部分。
所以左半由一个框架,我们将调用MainFrame
,即由2架,我们会打电话LabelFrame
和ButtonFrame
MainFrame
需要 385 像素宽,460 像素高。LabelFrame
应该是 375 像素宽,115 像素高。ButtonFrame
需要 375 像素宽,330 像素高。我的问题是我不知道如何将这些尺寸设置为框架。
我self.config(width = num, height = num)
显然已经尝试用适当的值替换 num ,但这没有做任何事情。
我知道窗口本身有一种.geometry
方法,但我无法找到 tk.Frame 的等效方法
使用grid_propagate(0)
或pack_propagate(0)
,取决于使用的几何管理器。0
只是False
,告诉 tkinter 关闭几何传播。
默认情况下,传播处于开启状态,并且容器会增长/缩小到刚好足以容纳其内容。
我假设你想要的布局是这样的:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
root = tk.Tk()
MainFrame = tk.Frame(root, width=385, height=460, relief='raised', borderwidth=5)
LabelFrame = tk.Frame(MainFrame, width=375, height=115, relief='raised', borderwidth=5)
ButtonFrame = tk.Frame(MainFrame, width=375, height=330, relief='raised', borderwidth=5)
some_label = tk.Label(LabelFrame, text='Simple Text')
some_button = tk.Button(ButtonFrame, text='Quit', command=root.destroy)
for frame in [MainFrame, LabelFrame, ButtonFrame]:
frame.pack(expand=True, fill='both')
frame.pack_propagate(0)
for widget in [some_label, some_button]:
widget.pack(expand=True, fill='x', anchor='s')
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
和grid
经理的区别仅在循环部分(注意sticky
和row/column configure
):
...
for frame in [MainFrame, LabelFrame, ButtonFrame]:
# sticky='nswe' acts like fill='both'
frame.grid(sticky='nswe')
frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
frame.grid_propagate(0)
for widget in [some_label, some_button]:
# sticky='wse' acts like fill='x' + anchor='s'
widget.grid(sticky='wse')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
...
Run Code Online (Sandbox Code Playgroud)