简体   繁体   中英

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. but a label doesn't appear on the frame.

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. You should not be using the custom style for the label widget.

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. Thus, any text associated with the widget won't show.

You need to remove the style from the label widget:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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