python - 在 Tkinter Canvas 中创建 LabelFrame

标签 python python-2.7 tkinter

我试图在 Canvas 中放置一个显示 LabelLabelFrame 但是我收到此错误:

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

这是我的代码:

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()

最佳答案

制作 canvaslabel_frame 子项,并将 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')
...

anchor 默认为 CENTER。要正确对齐,请将 anchor 指定为 w

关于python - 在 Tkinter Canvas 中创建 LabelFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605405/

相关文章:

python - 相当于 pandas read_clipboard 的 NumPy?

python - 移动和合并矩阵的元素

Python/Pandas - 当 x 值为 "range"而不是整数时,将 y 值最小值设置为 0

python - Pandas 使用日期列通过 .shift 创建天列

python - 使用 pypyodbc 对列名进行 SQL Server 编码

python - 使用 Tkinter 将鼠标悬停在文本上时更改文本颜色?

python - 什么决定了 Tkinter Text 小部件的大小?

python-2.7 - Flask - Gevent 错误

arrays - Python 二维数组获取第一列等于0的所有行

python - 如何在 tkinter 中滚动到 TreeView 的底部