0

For instance, I have 2 frames and both are set to 300x300. On one of them, I put a widget in, and in the other one I don't. The one that I put a widget on just squeezed its size to the widget's size while the first frame kept its frame since its empty.

Can I get around this and have a fixed width and height for the frame no matter what I put?

An example:

from tkinter import *

root = Tk()
root.geometry("1000x1000")
frame1 = Frame(root, bg = "blue", width = 300, height = 300)
frame1.pack()

frame2 = Frame(root, bg = "red", width = 300, height = 300)
label = Label(frame2, text = "hey").pack()
frame2.pack()
acw1668
  • 40,144
  • 5
  • 22
  • 34
YarinB
  • 73
  • 1
  • 8
  • This is the default behavior - widgets shrink to fit their contents. You can defeat it, but it's almost never the right thing to do. Add widgets in their preferred size and tkinter will do a great job making the frame the best size it can be. – Bryan Oakley Mar 18 '21 at 20:14
  • Also the variable `label` is always `None`. For more info read [this](https://stackoverflow.com/a/66385069/11106801) – TheLizzard Mar 18 '21 at 20:35

1 Answers1

0

Check out this answer: tkinter's .pack_propagate() method

What you're looking for is pack_propagate

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
krose
  • 390
  • 1
  • 6
  • 18
  • 1
    In case you were wondering where to put it, add `frame2.pack_propagate(False)` on the line after `frame2 = Frame ...` – Henry Mar 18 '21 at 19:57
  • 1
    Thanks, I also found out that if using grid inside the frame then it should be grid_propagate(False) – YarinB Mar 19 '21 at 08:46