Julius Möller Julius Möller - 1 year ago 177
Python Question

Python Tkinter Label in Frame

I want to place a label inside a frame in tkinter, but I can't figure out how to actually get it inside.

import tkinter
from tkinter import *


FRAME=Frame(W, width=100, height =50).place(x=700,y=0)

LABEL=Label(FRAME, text="test").pack()

When I run this, it doesn't place the Label inside the frame, but just places it normally on the window.
What am I doing wrong?

Answer Source

In the line

FRAME=Frame(W, width=100, height =50).place(x=700,y=0)

You think you are returning a tk frame, but you are not! You get the return value of the place method, which is None

So try

frame = Frame(W, width=100, height=50)
frame.place(x=700, y=0)
label = Label(frame, text="test").pack()

If you don't want the frame to shrink to fit the label, use (How to stop Tkinter Frame from shrinking to fit its contents?)


Note: Either import tkinter or from tkinter import * but not both. Also, by convention, names of instances of objects are lowercase.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download