简体   繁体   English

如何让一个元素粘在Tkinter的右下角?

[英]How to get a element to stick to the bottom-right corner in Tkinter?

I put button on a frame like this in Tkinter for python3:我在 Tkinter 中为 python3 将按钮放在这样的框架上:

b2_1 = Button(frame1, text='aaaaaaaaaaaa')
b2_1.pack(pady=20, side=RIGHT)

b2_2 = Button(frame1, text='bbbbbbbbbbbbbbb')
b2_2.pack()

I want the buttons b2_1 and b2_2 to be stick not only to the right side, but also to the bottom, one right above another.我希望按钮 b2_1 和 b2_2 不仅要粘在右侧,还要粘在底部,一个在另一个的正上方。 How can I do that?我怎样才能做到这一点?

The answer to "how do I put something in the bottom right corner?" “如何在右下角放置东西?”的答案can't be adequately answered out of context.不能断章取意地回答。 It really depends on what else is in the window.这实际上取决于窗口中还有什么。 You can use use place to easily place a widget anywhere you want, but it's rarely the right solution.您可以使用place轻松地将小部​​件放置在您想要的任何位置,但这很少是正确的解决方案。

Grid is relatively easy to understand if you really do want to lay things out in a grid (eg: a right-most column with a couple rows on the bottom and one or more on top.如果您确实想在网格中布置内容,则网格相对容易理解(例如:最右侧的列,底部有几行,顶部有一行或多行。

Pack can also be used, though it typically involves using additional frames.也可以使用 Pack,尽管它通常涉及使用额外的帧。 For example, you could create a frame for the left and right, and then pack the buttons at the bottom of the right frame.例如,您可以为左侧和右侧创建一个框架,然后将右侧框架底部的按钮打包。

There is also the possibility to use more than one along with additional frames.还可以使用多个框架和附加框架。 For example, you could use grid to lay out the main widget into a header, main area, and footer, and then use pack to arrange buttons in the footer.例如,您可以使用 grid 将主要小部件布局为页眉、主要区域和页脚,然后使用 pack 排列页脚中的按钮。

If you literally only want two buttons, and you want them stacked in the bottom-right corner, I suggest using grid .如果您实际上只想要两个按钮,并且希望它们堆叠在右下角,我建议使用grid In addition to placing them in a row on the bottom, you need to make sure that there is at least one other row and one other column that takes up any extra space.除了将它们排在底部的一行之外,您还需要确保至少有另一行和另一列占用任何额外空间。 The rows and columns can be empty, but they must be configured to have a "weight".行和列可以为空,但它们必须配置为具有“权重”。

For example:例如:

frame1.grid_rowconfigure(0, weight=1)
frame1.grid_columnconfigure(0, weight=1)

# give the empty row 0 and column 0 a non-zero weight
# so that grid gives all extra space to those columns.

b2_1.grid(row=1, column=1)
b2_2.grid(row=2, column=1)

pack is not the ideal geometry manager in this case.在这种情况下, pack不是理想的几何管理器。 You use pack in a frame where you just want to stick children one after the other.您在一个框架中使用 pack ,您只想一个接一个地粘贴孩子。 There is also a place manager which I have yet to see a 'natural' use for.还有一个地方经理,我还没有看到它的“自然”用途。

In this case use the grid manager .在这种情况下使用网格管理器 This is less simple than pack but has the flexibility you want.这比 pack 简单,但具有您想要的灵活性。 Once you figure out how many rows and columns your grid will have, placing is simple with .grid(column=lastcol,row=lastrow) .一旦你弄清楚你的网格有多少行和列,使用.grid(column=lastcol,row=lastrow)放置很简单。 Terry Jan Reedy also suggested using way too big numbers since empty columns/rows are not displayed, but I like to be pedantic so I know how my grid looks like exactly. Terry Jan Reedy 还建议使用太大的数字,因为不会显示空的列/行,但我喜欢迂腐,所以我知道我的网格看起来如何。

The .gridsize() method of a frame/ window returns a tuple with the size of the grid.框架/窗口的 .gridsize() 方法返回一个具有网格大小的元组。 ie IE

    frame1.grid_size() = (number_of_columns_in_frame1, number_of_rows_in_frame1)

To put something in the bottom right of a frame use:要将某些东西放在框架的右下角,请使用:

    b2_1 = Button(frame1, text='aaaaaaaaaaaa')
    b2_1.grid(row=frame1.grid_size()[1], column=frame1.grid_size()[0])

This will add a row and column to the frame1's grid and place b2_1 in the new row and column (because grid's row/ column system is zero indexed).这将向 frame1 的网格添加一行和一列,并将 b2_1 放置在新的行和列中(因为网格的行/列系统是零索引的)。

Adding another button below it: Using the same system as above, ie在它下面添加另一个按钮:使用与上面相同的系统,即

    b2_2 = Button(frame1, text='bbbbbbbbbbbbbbb')
    b2_2.grid(row=frame1.grid_size()[1], column=frame1.grid_size()[0])

will create another new row and column and place b2_2 to the bottom right of b2_1.将创建另一个新行和列,并将 b2_2 放置在 b2_1 的右下角。 So to place b2_2 directly below b2_1(ie in the same column as b2_1) you have to decrease the column by 1 ie因此,要将 b2_2 直接放在 b2_1 下方(即与 b2_1 位于同一列中),您必须将该列减少 1,即

    b2_2 = Button(frame1, text='bbbbbbbbbbbbbbb')
    b2_2.grid(row=frame1.grid_size()[1], column=frame1.grid_size()[0]-1)

grid and pack don't work together and must not be used in the same python file. grid 和 pack 不能一起工作,不能在同一个 python 文件中使用。

this is the command to put a button on the bottom right using the pack() method.这是使用 pack() 方法在右下角放置一个按钮的命令。

btn.pack(side=BOTTOM, anchor="e", padx=8, pady=8)

Position the element within the grid cell - the positions are the same as a compass: N, E, S, W, NE, NW, SE, and SW.将元素定位在网格单元内 - 位置与指南针相同:N、E、S、W、NE、NW、SE 和 SW。

This example sticks it to the right bottom:这个例子把它贴在右下角:

example_label.grid(column=0, row=0,sticky =SE)

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

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