1

I feel like I am missing something very basic here. This is my first try creating a GUI in Python. I try to add several frames inside my main window and place stuff inside by using grid manager. The frame creation and sizing and stuff works fine, until I add anything inside the frame. Like in this example with only one frame, when I put my dummy label, the frame just completely vanishes and only the label is displayed.

I really want to understand what is going on and why it is behaving like it is ... and of course, how to fix it. Thanks a lot.

from tkinter import *
window = Tk()

# Window geometry
window.title("Tk")
window.geometry('850x600')

# Set Frames
LeftTopFrame = Frame(window,bg='red',height=200,width=300)
LeftTopFrame.pack(fill=X,side=LEFT)

lblInp = Label(LeftTopFrame, text='Label',font=('Tahoma', 10))
lblInp.grid(column=0,row=0)

window.mainloop()
Atd
  • 21
  • 4
  • 1
    That's how it's supposed to work - a Frame with contents will ignore its specified height/width and instead resize to fit its contents. You can turn this behavior off by calling `.pack_propagate(False)` on the Frame (there's also `grid_propagate`, if you're using grid instead of pack), but note that this will likely result in wasted space and/or chopped-off widgets. – jasonharper Dec 26 '18 at 19:04
  • Relevant [how-to-set-the-min-and-max-height-or-width-of-a-frame](https://stackoverflow.com/a/4399545/7414759) – stovfl Dec 26 '18 at 19:20
  • I have a post on another question that goes into some detail on the behavior of resizing using grid. It may help you understand what is going on with your frame as it has visuals to assist with understanding. [post](https://stackoverflow.com/a/50750928/7475225) – Mike - SMT Dec 26 '18 at 20:00
  • Thx for your replies guys. @jasonharper, the grid_propagate solution works well, while .pack_propagate doesn't seem to change anything in my setting. But I will look into that a bit more. I still find it odd, that I explicitly define dimensions and still those frames shrink. I assumed it would fit/resize when no size is given and stay as desired when defined in the code. I will also check out the other links! Thank you! – Atd Dec 27 '18 at 16:55

1 Answers1

0

Tkinter frames shrink or grow to fit their contents. Use

LeftTopFrame.grid_propagate(0)
Bitto
  • 7,937
  • 1
  • 16
  • 38