简体   繁体   English

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

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

I created new style frame using ttk.style And I made the frame appear with image.我使用 ttk.style 创建了新的样式框架,并使框架与图像一起出现。 but a label doesn't appear on the frame.但是框架上没有出现 label。

Here is my code:这是我的代码:

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

You are trying to apply the style to both the frame and the label inside your frame.您正在尝试将样式应用于框架和框架内的 label。 You should not be using the custom style for the label widget.您不应该为 label 小部件使用自定义样式。

The problem is that the layout specifies which internal elements appear for the widget and how they are organized.问题在于布局指定了小部件的哪些内部元素以及它们的组织方式。 A scrollbar has a trough and a thumb and a border.滚动条有一个槽、一个拇指和一个边框。 Buttons have text elements and borders, a Label has a border and a text element, etc. Your frame layout only has a border, it doesn't have a text element.按钮有文本元素和边框,Label 有边框和文本元素等。您的框架布局只有边框,没有文本元素。 Thus, any text associated with the widget won't show.因此,与小部件关联的任何文本都不会显示。

You need to remove the style from the label widget:您需要从 label 小部件中删除样式:

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

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

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