繁体   English   中英

tkinter ttk 样式在 tkinter 中,我想让我的 label 出现

[英]tkinter ttk style In tkinter, I want to make my label appear

我使用 ttk.style 创建了新的样式框架,并使框架与图像一起出现。 但是框架上没有出现 label。

这是我的代码:

root = tk.Tk()
style = ttk.Style()
img = tk.PhotoImage(file="img/line_test.png")
style.element_create("teststyle.TFrame", "image", img)
style.configure("teststyle.TFrame", background="red", compound="center")
style.layout("teststyle.TFrame", [("teststyle.TFrame", {"sticky": "nsew"})])
frame = ttk.Frame(root, style="teststyle.TFrame", height=100, width=360)
test_lbl = ttk.Label(frame, text="test", style="teststyle.TFrame")
test_lbl.pack()
frame.pack()
root.mainloop()

您正在尝试将样式应用于框架和框架内的 label。 您不应该为 label 小部件使用自定义样式。

问题在于布局指定了小部件的哪些内部元素以及它们的组织方式。 滚动条有一个槽、一个拇指和一个边框。 按钮有文本元素和边框,Label 有边框和文本元素等。您的框架布局只有边框,没有文本元素。 因此,与小部件关联的任何文本都不会显示。

您需要从 label 小部件中删除样式:

test_lbl = ttk.Label(frame, text="test")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM