Home > Enterprise >  tkinter deforms my frames after inserting objects
tkinter deforms my frames after inserting objects

Time:08-24

So I want to insert some objects in a frame, but when I firstly added a button the frames where were they weren't suppoused to.

Before
After

And this is the code:

import tkinter as tk

root = tk.Tk()
root.geometry("1200x700")

# Main frames
frame1 = tk.Frame(root, width=1200, height=625)
frame2 = tk.Frame(root, width=1200, bg="black", height=75)

frame1.grid(row=1, column=1)
frame2.grid(row=2, column=1)

# Secondary frames
frame1browser = tk.Frame(frame1, height=625, width=850, bg="grey")
frame1a = tk.Frame(frame1,height=625, width=(1200-850))

frame1browser.grid(row=1, column=1)
frame1a.grid(row=1, column=2)

# Last frames
frame1aa = tk.Frame(frame1a, width=(1200-850),height=525, bg="green")
frame1ab = tk.Frame(frame1a, width=(1200-850),height=100, bg="yellow")

frame1aa.grid(row=1, column=1)
frame1ab.grid(row=2, column=1, sticky="nswe")

# Elements that are not frames
Button1 = tk.Button(frame1ab, text="ur mother")
Button1.grid(column=1, row=1)

root.mainloop()

CodePudding user response:

The frame ignores the width/height explicitly given if there is a widget inside it, by default. AFAIK, It finds and uses the minimum size required to fit all the widgets, also accommodating to extra properties like sticky, expand and so on.

To override this behavior, you will have to use the <grid/pack>_propagate(False) depending on whether you use pack or grid on the items inside the frame. Now the frame will grow/shrink as much as the size you specify.

frame1ab.grid_propagate(False)
  • Related