在Tkinter Canvas中创建LabelFrame

K D*_*awG 5 python tkinter python-2.7

我正在尝试放置一个LabelFrame显示Label内部的Canvas但是我收到此错误:

TclError:不能在此画布的窗口项中使用.28425672.27896648

这是我的代码:

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()
Run Code Online (Sandbox Code Playgroud)

fal*_*tru 3

制作label_frame的子项canvas,并将其打包label到框架内。然后传递label_frame(而不是label)到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')
...
Run Code Online (Sandbox Code Playgroud)

anchorCENTER默认的。要正确对齐,请将锚点指定为w