简体   繁体   English

如何在使用tkinter和pack重新调整python中具有框架的窗口时冻结窗格?

[英]How to Freeze a pane while re-sizing a window that has frames in python using tkinter and pack?

This is my first post on stackoverflow. 这是我在stackoverflow上的第一篇文章。 I am finally posting because I can not find this anywhere and have been searching for nearly 4 hours, but I am stuck. 我终于发帖了,因为我无法在任何地方找到这个并且已经搜索了近4个小时,但我被卡住了。

Here is my code example: 这是我的代码示例:

import tkinter as tk
from tkinter import *
root = tk.Tk()
root.geometry("600x100+200+200")
leftverticalFrame = Frame(root)
leftverticalFrame.pack(side=LEFT)
middleverticlFrame = Frame(root)
middleverticlFrame.pack(expand=TRUE)
rightverticalFrame = Frame(root)
rightverticalFrame.pack(side=RIGHT)

right = tk.Label(rightverticalFrame, text="Right Vertical Status Frame", bg="yellow")
right.pack(side=tk.RIGHT, fill=BOTH)

left = tk.Label(leftverticalFrame, text = "Left Vertical Navigation Frame", bg="orange")
left.pack(side=tk.LEFT, fill=BOTH)

bottom = tk.Label(middleverticlFrame, text="Middle Vertical Frame", bg="blue")
bottom.pack(side=tk.BOTTOM, expand=True, fill=tk.BOTH)

root.mainloop()

What I am doing is merely trying to layout the frames individually within the root because the frames will use different managers. 我正在做的只是尝试在根目录中单独布局框架,因为框架将使用不同的管理器。 The left frame is functioning exactly as I want it to, as is the middle frame. 左框架的功能与我想要的完全一样,中间框架也是如此。 The problem is with the frame on the right. 问题出在右边的框架上。

Notice when you re-size the window making it more narrow, the right frame comes into the "middle frame's territory". 请注意,当您重新调整窗口大小使其更窄时,右侧框架将进入“中间框架的区域”。 Now the strange thing is the middle frame does not replicate the same behavior when it comes to the boundary of the left frame. 现在奇怪的是,当涉及到左边框的边界时,中间框架不会复制相同的行为。 I want the right frame to behave the same as the middle frame. 我希望右框架的行为与中间框架相同。 Essentially I am trying to make the Left and Right fairly static, but the middle frame more dynamic. 本质上我试图使左和右相当静态,但中间框架更动态。 Can anyone tell me what I am doing wrong please? 任何人都可以告诉我,我做错了吗?

An important thing to remember about pack is that the side attribute doesn't refer to the side of the window, it refers to the side of the remaining available space . 关于pack一个重要事项是side属性不是指窗口的一侧,而是指剩余可用空间一侧 The causes the order in which you pack things and the side that you pack them to be significant, because each time you pack something you change the location and amount of remaining available space. 导致包装物品的顺序和包装物品的一面变得非常重要,因为每次包装物品都会改变剩余可用空间的位置和数量。

In this case, the problem is that you didn't specify the side attribute for the middle frame, so it defaults to "top" (as in, "top of the remaining space", not "top of the window"). 在这种情况下,问题是您没有为中间框架指定side属性,因此它默认为"top" (如“剩余空间的顶部”, 而不是 “窗口的顶部”)。 Since there's already something on the left, this puts it at the top of the remaining space on the right. 由于左侧已有东西,因此将其置于右侧剩余空间的顶部。 Then, when you put the next item on the right, it's on the right but below the thing that is on the top. 然后,当您将下一个项目放在右侧时,它位于右侧但位于顶部的物体下方。

There are at least a couple ways to solve this. 至少有几种方法可以解决这个问题。 The first is to pack the left and right sides first, and then pack the middle. 首先是首先打包左侧和右侧,然后打包中间。 In this case it doesn't matter which side you put the middle frame: 在这种情况下,放置中间框架的哪一侧无关紧要:

leftverticalFrame.pack(side=LEFT)
rightverticalFrame.pack(side=RIGHT)
middleverticlFrame.pack(expand=TRUE, side=TOP)

The second solution is to leave them in the original order, but pack the middle frame on the left or right instead of the top: 第二个解决方案是按原始顺序保留它们,但是将中间框架包装在左侧或右侧而不是顶部:

leftverticalFrame.pack(side=LEFT)
middleverticlFrame.pack(expand=TRUE, side=LEFT)
rightverticalFrame.pack(side=RIGHT)

These two variations will initially look identical, or perhaps nearly identical depending on what else might be in the frames or in the window. 这两个变体最初看起来相同,或者几乎相同,这取决于框架或窗口中的其他内容。 However, the behavior is different when you start to make the window too small to fit all of the frames. 但是,当您开始使窗口太小而无法容纳所有帧时,行为会有所不同。

In such a case, tkinter must eventually start reducing the size of a widget. 在这种情况下,tkinter最终必须开始减小小部件的大小。 It does this in the reverse order that they were packed (read: the last one to be packed is the first one to be shrunk). 它按照它们打包的相反顺序执行此操作(读取:要打包的最后一个是要收缩的第一个)。 That means that if you want the left and right to be fixed as much as possible, you should pack the middle section last. 这意味着如果你想要尽可能多地修复左边和右边,你应该最后打包中间部分。


pro tip: it makes your code easier to read and maintain if you group all of your layout code together. 专业提示:如果将所有布局代码组合在一起,它可以使您的代码更易于阅读和维护。 Consider this code: 考虑以下代码:

f1 = Frame(...)
f1.pack(...)
f2 = Frame(...)
f2.pack(...)

I think you'll find over time that your code is easier to read and maintain if you write it like this: 我想你会发现,如果你这样编写代码,你的代码会更容易阅读和维护:

f1 = Frame(...)
f2 = Frame(...)
...
f1.pack(...)
f2.pack(...)
...

I think it makes the code much easier to visualize, since all of the layout for a given parent window is in one place rather than sprinkled throughout the code. 我认为它使代码更容易可视化,因为给定父窗口的所有布局都在一个地方而不是遍布整个代码。

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

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