如何冻结窗格,同时使用tkinter和pack重新调整python框架中的窗口大小?(How to Freeze a pane while re

编程入门 行业动态 更新时间:2024-10-23 20:19:58
如何冻结窗格,同时使用tkinter和pack重新调整python框架中的窗口大小?(How to Freeze a pane while re-sizing a window that has frames in python using tkinter and pack?)

这是我在stackoverflow上的第一篇文章。 我终于发帖,因为我找不到任何地方,并已搜索近4个小时,但我被卡住了。

这是我的代码示例:

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

我所做的仅仅是尝试在根中单独布置帧,因为帧将使用不同的管理器。 左框架的功能与我想要的完全一样,正如中框一样。 问题在于右边的框架。

注意,当您重新调整窗口的大小以使其更窄时,正确的框架会进入“中间框架的区域”。 现在奇怪的是,中间框架在左边框的边界上不会复制相同的行为。 我希望正确的框架与中间框架的行为相同。 本质上,我试图使左右相当静态,但中间框架更具动态性。 任何人都可以告诉我我做错了吗?

This is my first post on stackoverflow. I am finally posting because I can not find this anywhere and have been searching for nearly 4 hours, but I am stuck.

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?

最满意答案

关于pack一个重要事情是, side属性并不指向窗口的边,它指的是剩余可用空间一侧 。 原因包装的顺序和包装它们的方面都很重要,因为每次包装东西时,都会改变剩余可用空间的位置和数量。

在这种情况下,问题在于您没有为中间框指定side属性,所以它默认为"top" (如在“剩余空间的顶部”, 而不是 “顶部的窗口”)。 由于左侧已经有东西,因此将其放在右侧剩余空间的顶部。 然后,当您将下一个项目放在右侧时,它位于右侧,但位于顶部之下。

至少有一些方法可以解决这个问题。 首先是先打包左侧和右侧,然后打包中间。 在这种情况下,放置中间框架的哪一方并不重要:

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

第二种解决方案是按照原始顺序放置它们,但将中间框架放在左侧或右侧而不是顶部:

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

这两个变化最初看起来是相同的,或者也许几乎相同,这取决于帧或窗口中可能存在的内容。 但是,当您开始使窗口太小而不适合所有框架时,行为会有所不同。

在这种情况下,tkinter最终必须开始减小小部件的大小。 它以相反的顺序完成它们被打包(读取:最后一个被打包的是第一个被缩小的)。 这意味着如果你想要尽可能地固定左边和右边,你应该把中间部分放在最后。


专业提示:如果将所有布局代码组合在一起,它将使您的代码更易于阅读和维护。 考虑这个代码:

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

我想你会发现随着时间的推移,你的代码更容易阅读和维护,如果你这样写:

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

我认为它使代码更易于可视化,因为给定父窗口的所有布局都位于同一位置,而不是散布在整个代码中。

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

更多推荐

本文发布于:2023-07-15 18:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   窗格   大小   窗口   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!