Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 962
K DawG Avatar asked Oct 21 '22 22:10

K DawG


1 Answers

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.

like image 145
falsetru Avatar answered Oct 24 '22 02:10

falsetru