简体   繁体   中英

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:

在此处输入图像描述

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: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM