简体   繁体   English

结合网格/包Tkinter

[英]Combining grid/pack Tkinter

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). 我知道过去网格和包装上有很多问题,但我只是不明白如何将两者结合起来,因为我在两个方向(行/列)上扩展我的'表'时遇到了困难。

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. 将'win1'改为打包工作的意义在于它保持中心但就是这样。

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

Your first problem is that the main frame, win1 is packed with no options. 你的第一个问题是主框架win1没有选项。 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填充整个窗口(假设它实际上是你想要它做的):

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

That will cause this frame to properly expand and shrink when you resize the window. 当您调整窗口大小时,这将导致此框架正确扩展和缩小。

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. 第二个问题是你在win权重为1时给第0行,但是你把第3行中的frame_table放在默认权重为0的情况下。我不知道这是否有意,但这就是保持标签和条目小部件粘在屏幕的底部,因为win1的空行0正在扩展和缩小以占用额外的空间。

How to learn to lay out your widgets 如何学习布置你的小部件

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. 也许顶部的东西(例如工具栏)应该保持在顶部等。通常,只有一个区域是可扩展的,尽管可扩展区域本身可以分成两个或更多个区域。

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. 如果你有一个简单的布局(每个区域都是从上到下或从左到右),那么pack非常棒,如果你真的有一个网格,网格很棒。 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. 要有条不紊,将您的设计布置在纸上,转移到具有不同颜色的框架,然后添加您真实的小部件并添加最终的抛光。

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

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