Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Tkinter从pack()切换到grid()方法时重新缩放帧大小_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x Tkinter从pack()切换到grid()方法时重新缩放帧大小

Python 3.x Tkinter从pack()切换到grid()方法时重新缩放帧大小,python-3.x,tkinter,Python 3.x,Tkinter,我需要我的界面如下所示: 每种颜色都是一个单独的框架,在每个框架内我定义了一些标签 from tkinter import * root = Tk() root.title("Library program") root.geometry('{}x{}'.format(900, 450)) root.resizable(width=False, height=False) #define main containers topFrame = Frame(root, width=450, h

我需要我的界面如下所示:

每种颜色都是一个单独的框架,在每个框架内我定义了一些标签

from tkinter import *

root = Tk()
root.title("Library program")
root.geometry('{}x{}'.format(900, 450))
root.resizable(width=False, height=False)

#define main containers
topFrame = Frame(root, width=450, height=50, pady=3)
bottomFrame = Frame(root, width=450, height=50)

#main container layouts
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)

topFrame.grid(row=0, sticky="nsew")
bottomFrame.grid(row=1, sticky="nsew")

#define additional containers
bottomFrame.grid_rowconfigure(0, weight=1)
bottomFrame.grid_columnconfigure(1, weight=1)

bottomLeft = Frame(bottomFrame, width=225, bg="red")
bottomMidLeft = Frame(bottomFrame, width=225, bg="blue")
bottomMidRight = Frame(bottomFrame, width=225, bg="yellow")
bottomRight = Frame(bottomFrame, width=225, bg="green")

bottomLeft.pack_propagate(0)
bottomMidLeft.pack_propagate(0)
bottomMidRight.pack_propagate(0)
bottomRight.pack_propagate(0)

bottomLeft.grid(row=0, column=0, sticky = "nsew")
bottomMidLeft.grid(row=0, column=1, sticky = "nsew")
bottomMidRight.grid(row=0, column=2, sticky = "nsew")
bottomRight.grid(row=0, column=3, sticky = "nsew")

#top container widgets
titleLabel = Label(topFrame, text="Jakub's Library Program", font="Calibri 20")
titleLabel.pack()

#bottom container widgets
leftLabel = Label(bottomLeft, text="Book Search", font="Calibri")
midLeftLabel = Label(bottomMidLeft, text="Book Checkout", font="Calibri")
midRightLabel = Label(bottomMidRight, text="Return Books", font="Calibri")
rightLabel = Label(bottomRight, text="Popular Books", font="Calibri")

leftLabel.grid(row=0, column=0)
midLeftLabel.pack()
midRightLabel.pack()
rightLabel.pack()

bookTitleLabel = Label(bottomLeft, text="Book title", font="Calibri 12")
bookTitleEntry = Entry(bottomLeft)

bookTitleLabel.grid(row=1, column=0)
bookTitleEntry.grid(row=1, column=1)

root.mainloop()
但由于我需要像在红色框架中一样放置标签和条目,所以我使用grid()而不是pack()将标签放置在每个框架中

leftLabel.grid(row=0, column=0)
midLeftLabel.grid(row=0, column=0)
midRightLabel.grid(row=0, column=0)
rightLabel.grid(row=0, column=0)
现在我的界面如下所示:


使用grid()显示左下标签时没有问题,但由于某种原因,一旦使用grid()显示剩余标签,框架的大小就会变小。

如果您的目标是创建四个大小相等的列,
grid
使这非常容易。您需要确保每列具有相同的非零权重,并且需要为
uniform
选项为每列指定相同的值

您还应该删除对
pack\u propagate
的调用。关闭几何体传播很少是正确的做法。如果你想被要求做一系列的数学来确保所有的东西都适合你,这是一个不错的解决方案,但是让tkinter做所有的工作来确保所有的东西都适合你会更容易

删除此块:

bottomLeft.pack_propagate(0)
bottomMidLeft.pack_propagate(0)
bottomMidRight.pack_propagate(0)
bottomRight.pack_propagate(0)
添加这行代码:

bottomFrame.grid_columnconfigure((0,1,2,3), uniform="equal", weight=1)
注意:这将导致您的条目小部件被剪裁。这是因为您a)强制窗口为特定大小,b)将该大小划分为四个相等的列,c)使用入口小部件的默认大小,该大小太大,无法适应其他设计选择

更好的方法是不强制将窗口设置为特定大小。相反,让内容决定窗口的大小。您可以关注内部小部件的最佳大小,让tkinter来决定窗口需要多大

无论是哪种情况(强制窗口大小还是不强制窗口大小),都不需要为框架指定宽度,因为
grid
将调整它们的大小以适应需要


我还建议删除
root.resizeable(width=False,height=False)
。取消用户调整窗口大小的功能是一件非常用户友好的事情

你对此做过研究吗?这个网站上有很多关于网格的问题。大多数问题都与未正确配置行和/或列的权重有关。@BryanOakley我有,但我甚至不明白程序到底改变了什么,以便能够根据标签的网格更改帧的大小。特别是因为我使用了网格传播(0)。在你发布的代码中,你不能在任何地方调用
grid\u-propagate(0)
。@BryanOakley我的意思是打包传播(0)我的道歉。这很少是个好主意(使用
pack\u-propagate
)。你的最终目标是什么?您的目标是拥有四个大小相等的列吗?在第一个屏幕截图中,它们不是100%相等的,所以不完全清楚你想做什么。