Diogo Cosin
Diogo Cosin

Reputation: 107

How to make a widget occupy the same width as that of its master frame when using columnspan?

Given the following grid layout configuration:

enter image description here

It was generated through code:

from Tkinter import *

master = Tk()

frame1 = Frame(master, width=100, height=100, bg="red")
frame1.grid(sticky="nsew", columnspan=4)

frame2 = Frame(master, width=100, height=100, bg="blue")
frame2.grid()

frame3 = Frame(master, width=100, height=100, bg="green")
frame3.grid(row=1, column=1)

frame4 = Frame(master, width=100, height=100, bg="yellow")
frame4.grid(row=1, column=2)

frame4 = Frame(master, width=100, height=100, bg="purple")
frame4.grid(row=1, column=3)

master.mainloop()

I tried to insert a Entry in frame1 so it extends to the whole frame1 width with the following code:

e1 = Entry(frame1)
e1.grid(sticky="we", columnspan=4)

However, I have got the following result:

enter image description here

How can I make the Entry widget occupy the same width as the frame1?

Upvotes: 0

Views: 2018

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

The entry is inside frame1, so the grid it is in is completely unrelated to the grid in the root window.

Since you didn't tell tkinter what to do with extra space inside frame1, it left it unused. If you want the entry widget which is in column 0 to fill the frame, you need to give its column a weight.

frame1.grid_columnconfigure(0, weight=1)

If this is the only widget going in frame1, you might want to consider using pack since you can do it all in one line of code:

e1.pack(side="top", fill="x", expand=True)

Upvotes: 3

Related Questions