0

I want to have a frame and I need to pack labels inside it but when i do it just screws everything up.

frame_1 = Frame(root, height=125, width=250, bg="white", highlightthickness=4).pack()
def add_label():
   label = Label(frame_1, text="text", bg="black", fg="White").pack()

and when I run the programm it looks like this https://i.stack.imgur.com/LECa1.png note that the frame itself is white.

I don't understand why it won't pack it in the frame but instead below it...

Hinzu
  • 87
  • 1
  • 1
  • 4
  • 3
    One thing that is wrong is the `frame_1 = Frame(root, height=125, width=250, bg="white", highlightthickness=4).pack()` statement is assigning `None` to the variable `frame_1` because that's what `pack()` always returns. See [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name). – martineau Jan 01 '20 at 02:31
  • 1
    Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – stovfl Jan 01 '20 at 09:14
  • ***"I don't understand why it won't pack it in the frame"***: Your `frame_1` is `None` as described by @ martineau, therefore tkinter uses the default `root` as parent. Relevant [parent-master-vs-in-in-tkinter](https://stackoverflow.com/questions/46521814/parent-master-vs-in-in-tkinter) – stovfl Jan 01 '20 at 09:16

1 Answers1

1

Solution: pack_propagate(0) should do the trick: But before I have to say that Frame(root, height=125, width=250, bg="white", highlightthickness=4).pack() is wrong since it returns none. Use this:

frame_1 = Frame(root, height=125, width=250, bg="white", highlightthickness=4)
frame_1.pack()

Rest of the code:

frame_1.pack_propagate(0)    # this is the line that helps you

def add_label():
    label = Label(frame_1, text="text", bg="black", fg="White").pack()
add_label()

Tested on Python IDLE, v. 3.4.4

Hope that helped.

T. Feix
  • 179
  • 11