繁体   English   中英

如何在Python的Tkinter中更改按钮的颜色

[英]How do I change the color of a button in Python's Tkinter

我正在编写GUI,我想更改按钮的颜色,但是背景似乎更改了轮廓的颜色,而不是整个背景。 如何更改按钮背景的颜色?

我尝试了背景和风格。

salmon = "#FFC6AB"
black = "#0C120C"

# ADDING BUTTONS
# Adding style
style = ttk.Style()
style.configure("X.TFrame", background=grey)
style. configure("X.TButton", background=salmon, foreground=black, font=("Courier", 20), width=17)

# Adding spacer
spacer_1 = ttk.Frame(root)
spacer_1.grid(column=0, row=1, pady=12)
spacer_1.configure(style="X.TFrame")

# Adding frame
frame_2 = ttk.Frame(root)
frame_2.grid(column=0, row=2)
frame_2.configure(style="X.TFrame")

# Adding button 1
button_1 = ttk.Button(frame_2, text="Scale Cookbook")
button_1.grid(column=0, row=0)
button_1.configure(style="X.TButton")

您以正确的方式进行操作,您的代码确实更改了Linux中的按钮颜色。 您无法获得所需的结果,因为Windows和Mac的默认ttk主题不允许更改按钮的背景颜色(我想是因为它们是从图像创建的)。

如果要更改按钮的背景颜色,可以将主题的背景更改为“ clam”或“ alt”,例如:

style = ttk.Style()
style.theme_use('clam')
# ... the rest of your style configuration

暂无
暂无

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

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