简体   繁体   English

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

[英]Why does tkinter expand the frames disproportionately with respect to window resize?

In the following example, the 4 frames expand disproportionately when window is expanded.在以下示例中,当窗口扩展时,4 个框架会不成比例地扩展。 This is especially weird considering that the row and column configure of the frames is weighted 0.考虑到帧的行和列配置的权重为 0,这尤其奇怪。

What I want is to lock the size of frame 1 and frame 3 ie the frames on left and allow frame 2 to expand along x only while allowing frame 4 to expand in both x & y.我想要的是锁定第 1 帧和第 3 帧的大小,即左侧的帧,并允许第 2 帧仅沿 x 扩展,同时允许第 4 帧在 x 和 y 中扩展。 here is the code:这是代码:

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

Now the following code is expanding sort of how I want, but adding space in between:现在下面的代码正在扩展我想要的方式,但在两者之间添加空间:

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

In the following example, the 4 frames expand disproportionately when window is expanded.在以下示例中,当窗口扩展时,4 个框架会不成比例地扩展。

The behavior is simply because you give all of the weight in the root window to row 0, column 0. That means that any extra unallocated space will be given to those positions in the root window.这种行为仅仅是因为您将根窗口中的所有权重赋予第 0 行第 0 列。这意味着任何额外的未分配空间都将分配给根窗口中的这些位置。

What I want is to lock the size of frame 1 and frame 3 ie the frames on left and allow frame 2 to expand along x only while allowing frame 4 to expand in both x & y.我想要的是锁定第 1 帧和第 3 帧的大小,即左侧的帧,并允许第 2 帧仅沿 x 扩展,同时允许第 4 帧在 x 和 y 中扩展。 here is the code:这是代码:

If you want the frames on the left to not be given extra space in the X direction, you need to set the weight of column zero to zero and give a weight of one to column one.如果不希望左侧的帧在 X 方向上有额外的空间,则需要将第 0 列的权重设置为零,并为第 1 列赋予权重 1。 That will cause column one to receive all extra space.这将导致第一列获得所有额外空间。

For the rest of the problem, one option is to give all of the row weight to row one, and have frame three "stick" only to the top and the sides while frame four sticks to all sides.对于问题的其余部分,一种选择是将所有行权重赋予第一行,并使第三帧仅“粘”到顶部和侧面,而第四帧则“粘”到所有边。

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")

A second option to prevent frame three from resizing in the Y direction is to set a weight of zero for row zero and one, and then give a weight of one to a third row.防止第三帧在 Y 方向上调整大小的第二个选项是将第 0 行和第 1 行的权重设置为零,然后将权重设置为第三行。 You can then have frame four span two rows so that it gets the space allocated to row one plus all of the space allocated to row two.然后,您可以让第四帧跨越两行,以便它获得分配给第一行的空间加上分配给第二行的所有空间。

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

Here's a complete working example using the first technique, with the grid commands grouped together.这是使用第一种技术的完整工作示例,将网格命令组合在一起。 My experience has taught me that grouping all of the layout code within a given container makes it much easier to visualize and maintain layout code.我的经验告诉我,将给定容器中的所有布局代码分组可以更容易地可视化和维护布局代码。

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

Finally got what I wanted, but still feels weird that I had to define a lot more cells than I needed.终于得到了我想要的东西,但仍然感觉很奇怪,我必须定义比我需要的多得多的单元格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM