Home > Back-end >  frame.grid_propogate(False) not working properly
frame.grid_propogate(False) not working properly

Time:04-21

Result of following code is a blank window :

   import tkinter as tk
   from tkinter import Tk, Grid, Frame

   root = Tk()
   root.geometry("70x80")

   Grid.rowconfigure(root, index=0, weight=1)
   Grid.columnconfigure(root, index=0, weight=1)


   topRowFrame= Frame(root)
   Grid.rowconfigure(topRowFrame, index=0, weight=1)
   Grid.columnconfigure(topRowFrame, index=0, weight=1)
   topRowFrame.grid(column=0,row=0)
   bookingIdLabelDiag=tk.Label(topRowFrame, text='Booking ID')
   bookingIdLabelDiag.grid(column=0, row=0)
   topRowFrame.grid_propagate(False)
  
   root.mainloop()

Label text 'Booking ID' doesn't appear. It works fine when I comment grid_propopagate line. Please help.

CodePudding user response:

You haven't set the dimensions of the frame, so when you disable content-fitting, the frame collapses.

w.grid_propagate() Normally, all widgets propagate their dimensions, meaning that they adjust to fit the contents. However, sometimes you want to force a widget to be a certain size, regardless of the size of its contents. To do this, call w.grid_propagate(0) where w is the widget whose size you want to force.

    topRowFrame = Frame(root, width=65, height=70,  bg='red')
    

CodePudding user response:

What you are seeing is definitely because grid_propagate is working. This says "don't let the frame grow or shrink to fit its contents" Because you didn't give the frame a size, it's going to default to 1x1. The widgets are there, but since the frame is only 1 pixel wide and tall you can't see them.

If you give the frame a width and a height, or if you use sticky to force the frame to fill the space allocated to it, they will show up.

topRowFrame.grid(column=0,row=0, sticky="nsew")

This is a good illustration of why you should almost never turn geometry propagation off. Tkinter is very good at making widgets fit. When you turn that off, you need to be more careful about how you try to make them visible.

  • Related