为什么在使用 python tkinter 时会调整帧大小? [英] Why are frames resizing when using python tkinter?

查看:23
本文介绍了为什么在使用 python tkinter 时会调整帧大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

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:

无标签

但是,当我将代码保留在下面(带有标签)时,我得到了完全不同的结果.看起来好像框架被调整为另一种尺寸而不是我选择的尺寸并且颜色消失了.这是为什么?

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?

带标签

推荐答案

这就是 tkinter 的设计初衷.当您使用 packgrid 时,框架(或任何其他小部件)将缩小或扩大以尝试适应其所有内容.

That is simply how tkinter was designed to work. When you use pack or grid, frames (or any other widget) will shrink or expand to try to fit all of its contents.

99.9% 的情况下,这是您想要的行为.Tkinter 非常擅长将 GUI 制作成合适的大小.

99.9% of the time, this is the behavior you want. Tkinter is really good at making GUIs the appropriate size.

来自 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. 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.

来自 pack 的文档:

packer 通常会计算 master 必须有多大才能恰好满足其 slave 的需求,并将 master 的请求宽度和高度设置为这些尺寸.这导致几何信息通过窗口层次向上传播到顶级窗口,以便整个子树调整自身大小以适应叶窗口的需要.然而,包传播命令可用于关闭一个或多个主机的传播.如果传播被禁用,那么打包器将不会设置打包器请求的宽度和高度.例如,如果您希望主窗口具有您指定的固定大小,这可能很有用.

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.

注意 place 没有相同的行为.来自 place 文档:

Notice that place doesn't have the same behavior. From the place documentation:

与许多其他几何管理器(例如打包器)不同,布局器不会尝试操纵主窗口或从窗口的父窗口的几何图形(即,它不会设置它们请求的大小).要控制这些窗口的大小,请使它们像框架和画布一样提供用于此目的的配置选项的窗口.

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 (i.e. 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.

这篇关于为什么在使用 python tkinter 时会调整帧大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆