Python 为什么tkinter在调整窗口大小时会不成比例地扩展框架?

Python 为什么tkinter在调整窗口大小时会不成比例地扩展框架?,python,python-3.x,tkinter,window,Python,Python 3.x,Tkinter,Window,在下面的示例中,当窗口展开时,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,weig

在下面的示例中,当窗口展开时,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()

终于得到了我想要的,但仍然觉得奇怪,我必须定义比我需要的更多的细胞

import tkinter as tk
from tkinter import ttk

def about_info():
    pass

root = tk.Tk()
#root.geometry('300x300')
root.rowconfigure(0,weight=0)
root.columnconfigure(0,weight=0)

root.rowconfigure(1,weight=0)
root.columnconfigure(1,weight=1)

root.rowconfigure(2,weight=0)
root.rowconfigure(3,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="ew")
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="ew")
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="ewn")
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=3, sticky="ewns")
lbl4 = ttk.Label(f4, text="Label4")
lbl4.grid(row=0,column=0, sticky="ew")

f5 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 5")
f5.grid(row=2, column=0, columnspan=1, rowspan=1, sticky="ewns")
lbl5 = ttk.Label(f5, text="Label5")
lbl5.grid(row=0,column=0, sticky="ewn")

root.mainloop()

终于得到了我想要的,但仍然觉得奇怪,我必须定义比我需要的更多的细胞

import tkinter as tk
from tkinter import ttk

def about_info():
    pass

root = tk.Tk()
#root.geometry('300x300')
root.rowconfigure(0,weight=0)
root.columnconfigure(0,weight=0)

root.rowconfigure(1,weight=0)
root.columnconfigure(1,weight=1)

root.rowconfigure(2,weight=0)
root.rowconfigure(3,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="ew")
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="ew")
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="ewn")
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=3, sticky="ewns")
lbl4 = ttk.Label(f4, text="Label4")
lbl4.grid(row=0,column=0, sticky="ew")

f5 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 5")
f5.grid(row=2, column=0, columnspan=1, rowspan=1, sticky="ewns")
lbl5 = ttk.Label(f5, text="Label5")
lbl5.grid(row=0,column=0, sticky="ewn")

root.mainloop()
在下面的示例中,当窗口展开时,4个帧会不成比例地展开

这种行为很简单,因为您将根窗口中的所有权重赋给了第0行第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()
如果希望左侧的帧在X方向上没有额外的空间,则需要将列0的权重设置为零,并为列1指定权重。这将导致列1接收所有额外空间

对于问题的其余部分,一个选项是将所有行的权重赋给第一行,并将第三帧“粘住”仅放在顶部和侧面,而第四帧粘住所有侧面

root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)
...
f3.grid(row=1, column=0, sticky="new")
...
f4.grid(row=1, column=1, sticky="nwes")
防止第三帧在Y方向上调整大小的第二个选项是为第0行和第1行设置0的权重,然后为第三行设置1的权重。然后,您可以让第四帧跨越两行,以便它获得分配给第一行的空间加上分配给第二行的所有空间

root.columnconfigure(1, weight=1)
root.rowconfigure(2, weight=1)
...
f4.grid(row=1, column=1, rowspan=2, sticky="nwes")

下面是使用第一种技术的完整工作示例,其中网格命令分组在一起。我的经验告诉我,将所有布局代码分组到一个给定的容器中,可以更容易地可视化和维护布局代码

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

f1 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 1")
f2 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 2")
f3 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 3")
f4 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 4")

root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)

f1.grid(row=0, column=0, columnspan=1, rowspan=1, sticky="nwes")
f2.grid(row=0, column=1, columnspan=1, rowspan=1, sticky="nwes")
f3.grid(row=1, column=0, columnspan=1, rowspan=1, sticky="new")
f4.grid(row=1, column=1, 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.rowconfigure(0,weight=0)
f2.columnconfigure(0,weight=0)
lbl2 = ttk.Label(f2, text="Label2")
lbl2.grid(row=0,column=0, sticky="ew")

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.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()
在下面的示例中,当窗口展开时,4个帧会不成比例地展开

这种行为很简单,因为您将根窗口中的所有权重赋给了第0行第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()
如果希望左侧的帧在X方向上没有额外的空间,则需要将列0的权重设置为零,并为列1指定权重。这将导致列1接收所有额外空间

对于问题的其余部分,一个选项是将所有行的权重赋给第一行,并将第三帧“粘住”仅放在顶部和侧面,而第四帧粘住所有侧面

root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)
...
f3.grid(row=1, column=0, sticky="new")
...
f4.grid(row=1, column=1, sticky="nwes")
防止第三帧在Y方向上调整大小的第二个选项是为第0行和第1行设置0的权重,然后为第三行设置1的权重。然后,您可以让第四帧跨越两行,以便它获得分配给第一行的空间加上分配给第二行的所有空间

root.columnconfigure(1, weight=1)
root.rowconfigure(2, weight=1)
...
f4.grid(row=1, column=1, rowspan=2, sticky="nwes")

下面是使用第一种技术的完整工作示例,其中网格命令分组在一起。我的经验告诉我,将所有布局代码分组到一个给定的容器中,可以更容易地可视化和维护布局代码

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

f1 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 1")
f2 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 2")
f3 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 3")
f4 = ttk.LabelFrame(root, borderwidth="3", relief="ridge", text="Frame 4")

root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)

f1.grid(row=0, column=0, columnspan=1, rowspan=1, sticky="nwes")
f2.grid(row=0, column=1, columnspan=1, rowspan=1, sticky="nwes")
f3.grid(row=1, column=0, columnspan=1, rowspan=1, sticky="new")
f4.grid(row=1, column=1, 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.rowconfigure(0,weight=0)
f2.columnconfigure(0,weight=0)
lbl2 = ttk.Label(f2, text="Label2")
lbl2.grid(row=0,column=0, sticky="ew")

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.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()

阅读“允许第2帧沿x展开”:如果您只想
x
,请不要使用
f2.grid(…,sticky=“nwes”)
。框架的配置权重为0。按目前的情况,你想要什么就用什么。在传递到行时,如何计算框架网格的索引0:3或0.0:1.1?配置?“框架网格索引”:没有类似于
0:3
的范围,只有
元组(0,1,3)
表示行
0
,行
1
和行
3
。在我的示例中,我省略了行
2
。是否有某种方法可以定义每个单元格的不同行为,而不是应用于整行或整列。“每个单元格的行为”:如果网格布局不符合您的需要,请改为使用。阅读“允许第2帧沿x展开”:如果您只想
x,请不要使用
f2.网格(…,sticky=“nwes”)
。框架的配置权重为0。按目前的情况,你想要什么就用什么。在传递到行时,如何计算框架网格的索引0:3或0.0:1.1?配置?“框架网格索引”:没有类似于
0:3
的范围,只有
元组(0,1,3)
表示行
0
,行
1
和行
3
。在我的示例中,我省略了行
2
。是否有某种方法可以定义每个单元格的不同行为,而不是应用于整行或整列。“每个单元格的行为”:如果网格布局不适合您的需要,请改为使用。