简体   繁体   English

为什么在使用 python tkinter 时会调整帧大小?

[英]Why are frames resizing when using python tkinter?

My code is the following:我的代码如下:

import tkinter as tk

#setting up window.
root = tk.Tk()
root.title("CSV Maker")
root.geometry("600x300")

#setting up frames.
leftFrame = tk.Frame(root, bg="red", width=300, height=300)
rightFrame = tk.Frame(root, bg="blue", width=300, height=300)
#placing frames on window.
leftFrame.grid(row=0, column=0)
rightFrame.grid(row=0, column=1)

#setting up labels.
inputPathLabel = tk.Label(leftFrame, text="Input File Path:")

#placing labels on frames.
inputPathLabel.grid(row=0, column=0)
root.mainloop()

When I remove the label I get the following:当我删除标签时,我得到以下信息:

Without label无标签

However when I leave the code as it is below (with a label), I get a completely different result.但是,当我将代码保留在下面(带有标签)时,我得到了完全不同的结果。 It seems as if the frame was resized to another size than the one that I selected and the color is gone.好像框架被调整为另一种尺寸而不是我选择的尺寸并且颜色消失了。 Why is this?为什么是这样?

With label带标签

That is simply how tkinter was designed to work.这就是 tkinter 的设计工作方式。 When you use pack or grid , frames (or any other widget) will shrink or expand to try to fit all of its contents.当您使用packgrid ,框架(或任何其他小部件)将缩小或扩大以尝试适应其所有内容。

99.9% of the time, this is the behavior you want. 99.9% 的情况下,这是您想要的行为。 Tkinter is really good at making GUIs the appropriate size. Tkinter 非常擅长将 GUI 制作成合适的大小。

From the official documentation for grid :来自grid的官方文档:

The grid geometry manager normally computes how large a master must be to just exactly meet the needs of its slaves, and it sets the requested width and height of the master to these dimensions.网格几何管理器通常计算 master 必须有多大才能恰好满足其 slave 的需求,并将 master 的请求宽度和高度设置为这些尺寸。 This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows.这导致几何信息通过窗口层次向上传播到顶级窗口,以便整个子树调整自身大小以适应叶窗口的需要。 However, the grid propagate command may be used to turn off propagation for one or more masters.然而,网格传播命令可用于关闭一个或多个主机的传播。 If propagation is disabled then grid will not set the requested width and height of the master window.如果禁用传播,则网格将不会设置主窗口的请求宽度和高度。 This may be useful if, for example, you wish for a master window to have a fixed size that you specify.例如,如果您希望主窗口具有您指定的固定大小,这可能很有用。

From the documentation for pack :pack的文档中:

The packer normally computes how large a master must be to just exactly meet the needs of its slaves, and it sets the requested width and height of the master to these dimensions.打包机通常会计算主机必须有多大才能恰好满足其从机的需求,并将主机的请求宽度和高度设置为这些尺寸。 This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows.这导致几何信息通过窗口层次向上传播到顶级窗口,以便整个子树调整自身大小以适应叶窗口的需要。 However, the pack propagate command may be used to turn off propagation for one or more masters.然而,包传播命令可用于关闭一个或多个主机的传播。 If propagation is disabled then the packer will not set the requested width and height of the packer.如果传播被禁用,那么打包器将不会设置打包器请求的宽度和高度。 This may be useful if, for example, you wish for a master window to have a fixed size that you specify.例如,如果您希望主窗口具有您指定的固定大小,这可能很有用。

Notice that place doesn't have the same behavior.请注意,该place没有相同的行为。 From the place documentation:place文档:

Unlike many other geometry managers (such as the packer) the placer does not make any attempt to manipulate the geometry of the master windows or the parents of slave windows (ie it does not set their requested sizes).与许多其他几何管理器(例如打包器)不同,布局器不会尝试操纵主窗口或从窗口的父窗口的几何图形(即,它不设置它们请求的大小)。 To control the sizes of these windows, make them windows like frames and canvases that provide configuration options for this purpose.要控制这些窗口的大小,请使它们像框架和画布一样提供用于此目的的配置选项的窗口。

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

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