How to get a element to stick to the bottom-right corner in Tkinter?
Asked Answered
S

6

5

I put button on a frame like this in Tkinter for 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. How can I do that?

Sialkot answered 25/12, 2016 at 17:37 Comment(0)
S
5

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.

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

If you literally only want two buttons, and you want them stacked in the bottom-right corner, I suggest using 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)
Shrieve answered 25/12, 2016 at 23:30 Comment(0)
C
4

pack is not the ideal geometry manager in this case. You use pack in a frame where you just want to stick children one after the other. 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. Once you figure out how many rows and columns your grid will have, placing is simple with .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.

Catechumen answered 25/12, 2016 at 17:51 Comment(1)
I agree with using grid. 'Bottom right' means last column, not first ;-). Since empty rows and columns are not displayed, a row and column that are 'too large' also work. For instance, .grid(column=99, row=99) (or row=98 for button above) should usually work.Principal
B
0

The .gridsize() method of a frame/ window returns a tuple with the size of the grid. i.e.

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

Adding another button below it: Using the same system as above, i.e.

    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. So to place b2_2 directly below b2_1(i.e. in the same column as b2_1) you have to decrease the column by 1 i.e.

    b2_2 = Button(frame1, text='bbbbbbbbbbbbbbb')
    b2_2.grid(row=frame1.grid_size()[1], column=frame1.grid_size()[0]-1)
Buckwheat answered 15/9, 2020 at 13:0 Comment(0)
E
0

grid and pack don't work together and must not be used in the same python file.

this is the command to put a button on the bottom right using the pack() method.

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

Ensphere answered 7/4, 2022 at 16:0 Comment(0)
C
0

fateme tardasti's solution worked for me try it out:

btn.pack(side=BOTTOM, anchor="e", padx=8, pady=8)
Chaves answered 24/6, 2023 at 17:5 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMiraflores
B
-1

Position the element within the grid cell - the positions are the same as a compass: N, E, S, W, NE, NW, SE, and SW.

This example sticks it to the right bottom:

example_label.grid(column=0, row=0,sticky =SE)
Brita answered 25/5, 2020 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.