The label is not formed inside the frame, but occupies the entire root

I don't understand why the label isn't created inside the frame

from tkinter import *
root = Tk()

root.title('Метка внутри фрейма')
root.geometry('300x500+100+100')

frame_top=Frame(root, bg='#666', width = 300, height = 100)
frame_top.pack(side='top')

label_top = Label(frame_top, text='Текст метки', bg='green', fg='red', width=200, height=30, font='Arial 14 bold italic', justify=LEFT)
label_top.pack(side='bottom')

print(label_top.cget('width'),label_top.cget('height'))

root.mainloop()

enter a description of the image here

Author: gil9red, 2018-10-25

1 answers

Disable pack_propagate it does not allow widgets inside the frame to control the size of the frame itself.

from tkinter import *
root = Tk()

root.title('Метка внутри фрейма')
root.geometry('300x500+100+100')

frame_top=Frame(root, bg='#666', width = 400, height = 100)
frame_top.pack(side='top')
frame_top.pack_propagate(0)
label_top = Label(frame_top, text='Текст метки', bg='green', fg='red', width=20, height=5, font='Arial 14 bold italic', justify=LEFT)[![введите сюда описание изображения][1]][1]
label_top.pack(side='bottom')

print(label_top.cget('width'),label_top.cget('height'))

root.mainloop()

You can also use place where you can specify specific parameters for installing the widget

from tkinter import *
root = Tk()

root.title('Метка внутри фрейма')
root.geometry('300x500+100+100')

frame_top=Frame(root, bg='#666', width=400, height=200)
frame_top.pack(side='top')
label_top = Label(frame_top, text='Текст метки', bg='green', fg='red', font='Arial 14 bold italic', justify=LEFT)
label_top.place(x=120, y=150, w=150, h=50)

root.mainloop()
 1
Author: Twiss, 2018-10-30 09:38:35