【发布时间】:2020-01-25 22:46:11
【问题描述】:
在以下示例中,当窗口展开时,4 个框架不成比例地展开。 考虑到帧的行和列配置的权重为 0,这尤其奇怪。
我想要的是锁定第 1 帧和第 3 帧的大小,即左侧的帧,并允许第 2 帧仅沿 x 扩展,同时允许第 4 帧在 x 和 y 中扩展。 这是代码:
import tkinter as tk
from tkinter import ttk
def about_info():
pass
root = tk.Tk()
#root.geometry('300x300')
root.rowconfigure(0,weight=1)
root.columnconfigure(0,weight=1)
m = tk.Menu(root, relief='flat')
#m = tk.Menu(root, relief='ridge')
about = tk.Menu(m, relief='flat')
about.add_command(label='about', command=about_info)
m.add_cascade(label='help',menu=about)
root.config(menu=m)
f1 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 1")
f1.grid(row=0, column=0, columnspan=1, rowspan=1, sticky="nwes")
f1.rowconfigure(0,weight=0)
f1.columnconfigure(0,weight=0)
lbl1 = ttk.Label(f1, text="Label 1")
lbl1.grid(row=0,column=0, sticky="ew")
f2 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 2")
f2.grid(row=0, column=1, columnspan=1, rowspan=1, sticky="nwes")
f2.rowconfigure(0,weight=0)
f2.columnconfigure(0,weight=0)
lbl2 = ttk.Label(f2, text="Label2")
lbl2.grid(row=0,column=0, sticky="ew")
f3 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 3")
f3.grid(row=1, column=0, columnspan=1, rowspan=1, sticky="nwes")
f3.rowconfigure(0,weight=0)
f3.columnconfigure(0,weight=0)
lbl3 = ttk.Label(f3, text="Label3")
lbl3.grid(row=0,column=0, sticky="ew")
f4 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 4")
f4.grid(row=1, column=1, columnspan=1, rowspan=1, sticky="nwes")
f4.rowconfigure(0,weight=0)
f4.columnconfigure(0,weight=0)
lbl4 = ttk.Label(f4, text="Label4")
lbl4.grid(row=0,column=0, sticky="ew")
root.mainloop()
现在下面的代码正在扩展我想要的方式,但在两者之间添加了空格:
import tkinter as tk
from tkinter import ttk
def about_info():
pass
root = tk.Tk()
#root.geometry('300x300')
root.rowconfigure((0,0),weight=0)
root.columnconfigure((0,0),weight=0)
root.rowconfigure((0,1),weight=0)
root.columnconfigure((0,1),weight=1)
root.rowconfigure((1,0),weight=1)
root.columnconfigure((1,0),weight=0)
root.rowconfigure((1,1),weight=1)
root.columnconfigure((1,1),weight=1)
m = tk.Menu(root, relief='flat')
#m = tk.Menu(root, relief='ridge')
about = tk.Menu(m, relief='flat')
about.add_command(label='about', command=about_info)
m.add_cascade(label='help',menu=about)
root.config(menu=m)
f1 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 1")
f1.grid(row=0, column=0, columnspan=1, rowspan=1, sticky="wn")
lbl1 = ttk.Label(f1, text="Label 1")
lbl1.grid(row=0,column=0, sticky="ew")
f2 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 2")
f2.grid(row=0, column=1, columnspan=1, rowspan=1, sticky="nw")
lbl2 = ttk.Label(f2, text="Label2")
lbl2.grid(row=0,column=0, sticky="ew")
f3 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 3")
f3.grid(row=1, column=0, columnspan=1, rowspan=1, sticky="ns")
lbl3 = ttk.Label(f3, text="Label3")
lbl3.grid(row=0,column=0, sticky="ew")
f4 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 4")
f4.grid(row=1, column=1, columnspan=1, rowspan=1, sticky="nwes")
lbl4 = ttk.Label(f4, text="Label4")
lbl4.grid(row=0,column=0, sticky="ew")
root.mainloop()
【问题讨论】:
-
Read gui layout using frames and grid " 允许第 2 帧沿 x 扩展":如果您只想使用
X,请不要使用f2.grid(..., sticky="nwes"). 框架的配置权重为 0。:就目前而言,它对你想要的毫无用处。 -
在传递给 rowconfigure 时,如何计算此处 0:3 或 0.0 : 1.1 的框架网格索引?
-
“框架网格的索引”:没有像
0:3这样的范围,只有tuple(0, 1, 3)表示Row0,Row1和行3。在我的示例中,我省略了行2。 -
是否有办法为每个单元格定义不同的行为,而不是应用于整行或整列。
-
“每个单元格的行为”:如果网格布局不符合您的需求,请改用 The Tkinter Place Geometry Manager。
标签: python python-3.x tkinter window