In an effort to learn Tkinter, I've put together the program below. When I leave the button out of the interface the two frames are the same size when displayed. However, if the button is placed in either frame, the frame with the button shrinks.

import tkinter as tk
def main():
    main_win = tk.Tk()
    main_win.title('just playing around')
    main_win.geometry('350x450')

    main_win.rowconfigure(0, weight=1)
    main_win.rowconfigure(1, weight=1)
    main_win.columnconfigure(0, weight=1)
    main_win.columnconfigure(1, weight=1)
    main_win.columnconfigure(2, weight=1)

    frm1 = tk.Frame(main_win, bg='red', height=100, width=350)

    frm1.grid(row=0, sticky='nsew')
    frm1.rowconfigure(0, weight=1)
    frm1.rowconfigure(1, weight=1)
    frm1.rowconfigure(2, weight=1)
    frm1.columnconfigure(0, weight=1)

    frm2 = tk.Frame(main_win, bg='green', height=150, width=350)
    frm2.grid(row=1, sticky='nsew')
    frm2.rowconfigure(0, weight=1)
    frm2.rowconfigure(1, weight=1)
    frm2.rowconfigure(2, weight=1)
    frm2.columnconfigure(0, weight=1)

    quit_btn = tk.Button(frm1, bg='lightgrey', text='Quit',
                         command=main_win.destroy)
    quit_btn.grid(row=1)

    main_win.mainloop()

    return 0

if __name__ == '__main__':
    main()

Why is this happening, and what is needed to keep the frames the same size?

(Also, resizing the window causes the frames to resize vertically, but not horizontally. Why is that happening and how do I make the resizing happen in both directions (or turn it off in both directions)?)

1 Answers

1
Mike - SMT On

So because tkinter by default will force a frame to resize to the widgets even if you give it a size as soon as you place a widget in the frame it will resize to the widget. Instead we need to tell tkinter to not allow propagation. So if we disable propagation inside of the frame that holds the button it will no longer resize to fit the button.

For the issue of not expanding to the right when you resize that is because of the weights on column 1 and 2. Unless you have plans to add something to those columns you need to remove the weights for those columns.

Because you add a weight to all 3 columns the columns will resize at the same rate in the container. In this case because column 1 and 2 start out at size zero when you start to resize the window you will notice column 0 is resizing but not fast enough to fill the window. This is because column 1 and 2 are also growing at the same rate as column 0 due to the weights being set.

So to answer your comment you can keep the weights for column 1 and 2 if you plan on using those columns later and want those columns to also grow with the screen.

Try this code and let me know if you have any questions.

import tkinter as tk


def main():
    main_win = tk.Tk()
    main_win.title('just playing around')
    main_win.geometry('350x450')

    # main_win.rowconfigure(0, weight=1) # disable this
    main_win.rowconfigure(1, weight=1)
    main_win.columnconfigure(0, weight=1)
    # main_win.columnconfigure(1, weight=1) # disable this
    # main_win.columnconfigure(2, weight=1) # disable this

    frm1 = tk.Frame(main_win, bg='red', height=100, width=350)
    frm1.grid(row=0, column=0, sticky='nsew')

    frm1.grid_propagate(False) # add grid_propagate(False) after your grid placement.

    frm1.rowconfigure(0, weight=1)
    frm1.rowconfigure(1, weight=1)
    frm1.rowconfigure(2, weight=1)
    frm1.columnconfigure(0, weight=1)

    frm2 = tk.Frame(main_win, bg='green', height=150, width=350)
    frm2.grid(row=1, column=0, sticky='nsew')
    frm2.rowconfigure(0, weight=1)
    frm2.rowconfigure(1, weight=1)
    frm2.rowconfigure(2, weight=1)
    frm2.columnconfigure(0, weight=1)

    quit_btn = tk.Button(frm1, bg='lightgrey', text='Quit', command=main_win.destroy)
    quit_btn.grid(row=1)
    main_win.mainloop()


if __name__ == '__main__':
    main()

Results:

As you can see I can resize the bottom frame without the top frame resizing vertically and both frames expand horizontally now.

enter image description here