结合 grid/pack Tkinter [英] Combining grid/pack Tkinter

查看:26
本文介绍了结合 grid/pack Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道过去有很多关于 grid 和 pack 的问题,但我只是不明白如何将两者结合起来,因为我很难在两个方向(行/列)扩展我的表格".

I know there have been many questions on grid and pack in the past but I just don't understand how to combine the two as I'm having difficulties expanding my 'table' in both directions (row/column).

按钮我希望保持相同的大小,但始终位于窗口底部.表格"但是我希望通过调整窗口大小自动扩展,但似乎无法使其工作.将win1"更改为 pack 的工作原理是它保持中心位置,但仅此而已.

Buttons I wish to keep the same size but always stay at the bottom of the window. The 'table' however I wish to expand automatically with resizing the window but can't seem to make it work. Changing 'win1' to pack worked in the sense that it stays central but that's it.

我知道如何使用 pack 实现相同的效果,例如粘性等,我需要更改术语.

How can I achieve the same effects such as sticky etc with pack as I know I'll need to change the terminology.

代码如下(展示了基本的框架和几个小部件,不是完整的代码):

Code is as follows (showing basic frames and several widgets, not complete code):

root = Tk()  

win1 = Frame(root)
win1.pack()
win1.grid_columnconfigure(0, weight=1)
win1.grid_rowconfigure(1, weight=1)

frame_table = ttk.Frame(win1, style="Black.TLabel", relief='sunken', borderwidth=1)
frame_table.pack(row=2, column=0, padx=1, pady=1, sticky= "nsew")
frame_table.grid_columnconfigure(0, weight=1)
frame_table.grid_rowconfigure(1, weight=1)
text_table1 = Label(frame_table, text='Number1', bg='white', borderwidth=0)
text_table1.grid(row=1, column=0, sticky="nsew", padx=1, pady=1)
empty1 = Label(frame_table, bg='white', borderwidth=0)
empty1.grid(row=2, column=0, sticky="nsew", padx=1, pady=1)
text_table2 = Label(frame_table, text='Number2', bg='white', borderwidth=0, width=12)
text_table2.grid(row=1, column=1, sticky="nsew", padx=1, pady=1)
empty2 = Label(frame_table, bg='white', borderwidth=0)
empty2.grid(row=2, column=1, sticky="nsew", padx=1, pady=1)

frame_but = ttk.Frame(win1)
frame_but.grid(sticky=S, padx=1, pady=1)
frame_but.grid_columnconfigure(0, weight=1)
frame_but.grid_rowconfigure(1, weight=1)
but1 = ttk.Button(frame_but, text='Start', command=Start)
but1.grid(row=3, column=0, padx=2, pady=1, sticky="S")

推荐答案

您的第一个问题是主框架 win1 没有任何选项.默认是它填充它所在的容器部分.因此,无论你对内部小部件做什么,整个东西都将堆叠锚定到窗口的顶部.然后,您应该做的第一件事是告诉 win1 填满整个窗口(假设这实际上是您想要的):

Your first problem is that the main frame, win1 is packed with no options. The default is for it to not fill the part of its container that it is in. Thus, no matter what you do to the inner widgets, the whole thing will stack anchored to the top portion of the window. The first thing you should do, then, is tell win1 to fill the whole window (assuming that's actually what you want it to do):

win1.pack(side="top", fill="both", expand=True)

当您调整窗口大小时,这将导致此框架正确扩展和收缩.

That will cause this frame to properly expand and shrink when you resize the window.

第二个问题是,您给 win 中的第 0 行赋予了 1 的权重,但是您将 frame_table 放在第 3 行中,该行的默认权重为 0. 我不知道这是否是故意的,但这就是将标签和条目小部件粘在屏幕底部的原因,因为 win1 的空行 0 正在扩大和缩小以获取增加额外的空间.

The second problem is that you're giving row 0 in win a weight of 1, but you are putting frame_table in row 3 which has a default weight of 0. I don't know if that's intentional or not, but that is what keeps the labels and entry widgets stuck to the bottom of the screen, because the empty row 0 of win1 is expanding and shrinking to take up the extra space.

如何学习布置小部件

适当的调整大小行为很容易做到正确,但学习如何做到正确则相当困难.我的建议是,准备一些纸和一支铅笔.画出应用程序的主要区域——每个区域都有不同的属性.例如,沿着底部的一行应该留在底部(状态栏,或者可能是一排按钮).也许顶部(例如工具栏)应该保留在顶部的某些东西,等等.通常只有一个区域是可扩展的,尽管该可扩展区域本身可能被分成两个或多个区域.

Proper resize behavior is pretty easy to get right, but it's fairly hard to learn how to get it right. My recommendation is, get some paper and a pencil. Draw out the main regions of your application -- the areas that each have different properties. For example, a row along the bottom that should stay at the bottom (status bar, or row of buttons perhaps). Maybe something at the top (toolbar, for example) that should stay at the top, etc. Typically there will be only one region that is expandable, though that expandable region may itself be divided into two or more regions.

在这种情况下,我猜您有两个区域:一个表格和一行按钮.画出来很容易.接下来,为每个区域创建一个框架,并且为每个区域创建一个框架.为它们提供单独的背景颜色,并使用网格或包将它们放置在窗口中,无论哪种方式都能为您提供所需的调整大小行为.如果你有一个简单的布局(每个区域都是从上到下或从左到右粘贴),pack 很棒,如果你真的有一个网格,网格很棒.仅使用此方法,调整选项,直到获得主要区域所需的行为.不同的颜色将帮助您查看哪些区域正在调整大小,哪些没有.

In this case I'm guessing you have two regions: a table, and a row of buttons. Drawing this out is easy. Next, create a frame for each region, and only a frame for each region. Give them separate background colors, and place them in the window using grid or pack, whichever one gives you the resize behavior you want. pack is great if you have a simple layout (every region is sticked either top-to-bottom or left-to-right), grid is great if you truly have a grid. Work with just this, tweaking options until you get the behavior you want for the main regions. The different colors will help you see which areas are resizing and which are not.

一旦您让主要区域完全正确工作,您就可以开始关注内部部分.再次拿出铅笔和纸,对每个子区域做同样的事情.画出内部区域,并找出哪些区域会在其容器内生长,哪些不会.也许只有一个主要的子区域,因此您可以跳过这一部分.最后,如果您有子区域,请创建框架,再次为它们提供不同的颜色,以便您可以看到调整大小的内容.调整设置,直到一切都按照您想要的方式调整大小.起泡,冲洗,重复.

Once you have the main regions working exactly right, you can then start to focus on the inner portions. Get out that pencil and paper again, and do the same with each of these sub-regions. Draw out the inner regions, and figure out which ones will grow within their container and which ones will not. Maybe there's only one main sub-region so you can skip this part. Finally, create frames if you have sub-regions, again giving them different colors so you can see what is resizing. Tweak the settings until everything resizes just the way you want. Lather, rinse, repeat.

最后,您将无法再细分您的窗口.通常只有几个区域,所以这个过程很快.一旦你的程序的不同区域都按照你想要的方式调整大小,就可以添加实际的小部件了.完成后,您可以返回并从框架中删除颜色.

Finally, you will not be able to sub-divide your window any more. Usually there are only a couple of regions so this process is quick. Once you have the different regions of your program all resizing how you want, it's time to add the actual widgets. Once you've done that you can go back and remove the color from the frames.

这很简单,但需要有条不紊的方法.只是将一堆小部件扔到一个框架中并尝试随机的事情来使其工作 不是正确的方法.有条不紊,在纸上布置您的设计,转移到具有不同颜色的框架,然后添加您的真实小部件并添加最后的润色.

It's simple, but it requires a methodical approach. Just throwing a bunch of widgets into a frame and trying random things to get it to work is not the right approach. Be methodical, lay out your design on paper, transfer to frames with distinct colors, and then add your real widgets and add the final polish.

这篇关于结合 grid/pack Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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