Search code examples
pythontkinter

Python Tkinter; .place() not working but .grid() and .pack() do


I am trying to insert a Label with text on a frame. However, it does not appear with .place, even though it works fine with .grid and .pack.

label = tk.Label(master, text="test")
label.pack() # works
label = tk.Label(master, text="test")
label.grid() # works
label = tk.Label(master, text="test")
label.place(relx=.5, rely=.5) # doesn't work
label = tk.Label(master, text="test")
label.place(relx=.5, rely=.5, width=500, height=500) # doesn't work

To be clear, I am not mixing the 3 methods. I'm running each attempt by separately.

My code structure is as follows:

class Window(tk.Tk)
   def __init__(self):
      tk.Tk.__init__(self)
      frame_init = Frame_0(self, 0)
      frame_init.pack()

class Frame(tk.Frame)
   def __init__(self, Window)
      tk.Frame.__init__(self, Window)

class Frame_0(Frame):
   def __init__(self, Window)
      super().__init__(Window)
      label = tk.Label(self, text="test")
      label.METHOD()   # METHOD is pack, grid, or place ()

... somewhere else

Window = Window()
Window.mainloop()

Solution

  • When you use place, the size of the child won't affect the size of the master. Since you didn't specify a size for your frame its size will be 1x1. Your label is in the frame, but the frame is virtually invisible.

    If you're going to use place, it's up to you to make sure that you are placing the widget inside a visible widget.