Deelaka
Deelaka

Reputation: 13693

Creating a LabelFrame inside a Tkinter Canvas

I'm trying to place a LabelFrame which displays a Label inside a Canvas however I receive this error:

TclError: can't use .28425672.27896648 in a window item of this canvas

Here's my code:

from Tkinter import LabelFrame, Label, Tk, Canvas

root = Tk()

canvas = Canvas(root)
canvas.pack()

label_frame = LabelFrame(text="I'm a Label frame")
label = Label(label_frame,text="Hey I'm a Label")

canvas.create_window(10,20,window=label)

root.mainloop()

Upvotes: 5

Views: 4993

Answers (1)

falsetru
falsetru

Reputation: 369054

Make the label_frame child of the canvas, and pack the label inside the frame. Then pass label_frame (instead of label) to create_window .

...
label_frame = LabelFrame(canvas, text="I'm a Label frame")
label = Label(label_frame, text="Hey I'm a Label")
label.pack()

canvas.create_window(10, 20, window=label_frame, anchor='w')
...

anchor is CENTER by default. To correctly align, specify anchor as w.

Upvotes: 3

Related Questions