Jimmy Chang
Jimmy Chang

Reputation: 3

Tkinter: trouble filling entire frame with widget

I am attempting to create three frames: top, middle, and bottom. I successfully added widgets to my top and bottom frames and oriented them how I wanted them.

However, I am simply trying to add two entry widgets in the middle frame that will span across the entire width of the frame/window.

Due to the length of the code for all the widgets in the top and bottom frames, I'm just going to include a snippet of code for how my window and frames are configured:

root = Tk()
root.resizable(False, False)

top_frame = Frame(root)
middle_frame = Frame(root)
bottom_frame = Frame(root)

# I think this block is irrelavant to the question, but including it anyway just incase
rows = 0
while rows < 36:
    top_frame.rowconfigure(rows, weight=1)
    top_frame.columnconfigure(rows, weight=1)
    bottom_frame.rowconfigure(rows, weight=1)
    bottom_frame.columnconfigure(rows, weight=1)
    rows += 1

top_frame.grid(row=0, column=0)
middle_frame.grid(row=1, column=0)
bottom_frame.grid(row=2, column=0)

Here is the code I'm using for the two entry widgets:

entry_1 = Entry(middle_frame)
entry_2 = Entry(middle_frame)
entry_1.grid(row=0, column=0, sticky=E+W)
entry_2.grid(row=1, column=0, sticky=E+W)

However, they just stick to the center of the middle frame. I've tried many solutions but nothing seems to change how these entry widgets look within the frame--always centered, never changing size. I even tried just packing them and setting their fill to X. I'm probably overlooking something very simple, but I can't quite figure it out.

Here is a picture for reference

Upvotes: 0

Views: 711

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386010

The root of the problem is that the middle frame isn't configured to fill the full width of the window, so the widgets inside won't fill the full width of the window.

The first step is to use the sticky option on the middle window:

middle_frame.grid(row=1, column=0, sticky="ew")

Next, you haven't told grid what to do with extra space in the root window. As a rule of thumb any widget that uses grid to manage its children should have at least one row and one column with a weight greater than zero.

To get the middle frame to take up the full width of the window, give the column it is in a non-zero weight:

root.grid_columnconfigure(0, weight=1)

The same problem exists within the middle frame: you aren't instructing grid how to handle extra space. According to the rule of thumb we need to give column zero a weight within the middle frame:

middle_frame.grid_columnconfigure(0, weight=1)

That will allow the entry widgets in the middle frame to fill the middle frame, and we've configure the app so that the middle frame fills the full width of the window.

Upvotes: 2

Related Questions