Mo0nKizz
Mo0nKizz

Reputation: 322

Position Tkinter grid Python

I was doing some tests and there is something that I don't understand with grid.

This is my code :

from tkinter import *

root = Tk()

frame = Frame(root, bg='blue',width=200,height=200)
frame.grid(column=0,row=0, sticky="news")

frame2 = Frame(root, bg='red',width=200,height=200)
frame2.grid(column=1,row=0, sticky="news")

frame3 = Frame(root, bg='green',width=200,height=200)
frame3.grid(column=2,row=0, sticky="news")

frame4 = Frame(frame, bg='yellow',width=100,height=100)
frame4.grid(column=0,row=0,sticky="news")

frame4 = Frame(frame, bg='orange',width=100,height=100)
frame4.grid(column=0,row=1,sticky="news")

root.mainloop()

I wanted to have the orange and the yellow square inside my blue box. But as you can see when you run the code the blue box disappear behind the yellow and the orange. How can I solve this problem to get :

enter image description here

Upvotes: 1

Views: 95

Answers (1)

Stefan Scheller
Stefan Scheller

Reputation: 953

The blue frame is shrinking to the content of its children. You can avoid that by calling grid_propagate:

frame = Frame(root, bg='blue', width=200, height=200)
frame.grid(column=0,row=0, sticky="news")
frame.grid_propagate(0)

Result: enter image description here

Upvotes: 2

Related Questions