如何解决 ttk 按钮的样式问题?

How to fix styling problems with ttk button?

我的应用程序中有一个 TTK 按钮,我想为其设置样式,但问题是其中一些样式不起作用。这是我的按钮样式:

style.configure("Menu.TButton", font=("Times New Roman", 20), width=300, padx=20, anchor=W, background="#21252B", foreground="#fff", activebackground="#BD93F9", bd=0)

但是使用这种样式,foreground 只是在按钮上放置了一个白色覆盖层,background 根本不起作用,activebackground 也根本不起作用, padx 也不起作用。

这些样式的名称是不同的还是我做错了什么?

如果有帮助,这里是按钮的代码:

home_button = Button(menu_frame, style="Menu.TButton", text="Home")
home_button.grid(column=0, row=1)

外观如下:

感谢@j_4321 我找到了解决这个问题的方法。

默认 Windows 主题不允许更改按钮样式,因此要更改按钮样式,您需要使用 ttk 提供的主题,例如:style.theme_use("clam")

完成后,您可以使用配置更改按钮样式。

要在用户单击按钮或将鼠标悬停在按钮上时更改按钮的颜色,您可以使用 style.map,如下所示:

style.map("Menu.TButton", background=[('!active', "#21252B"), ('pressed', '#BD93F9'), ("active", "#282C34")], foreground=[('!active', '#fff'), ('pressed', '#fff'), ('active', '#fff')])

这里有一些您可以尝试的选项:

toolstyle = ttk.Style()  
toolstyle.theme_use("clam")    # "default", "alt" .....

toolstyle.configure('TButton',                                           
                     background="black",
                     foreground="white", 
                     borderwidth=1,
                     bordercolor="red",
                     lightcolor="yellow",
                     darkcolor="purple",
                     focuscolor="none", 
                     font=("Bahnschrift", 14),
                     width=2)
toolstyle.map('TButton',
                    background=[("pressed", "white"),
                                ("active", "grey")],
                    borderwidth=[("active", 0)],
                    bordercolor=[("active", "blue")],
                    lightcolor=[("active", "purple")],
                    darkcolor=[("active", "black")],
                    foreground=[("pressed", "black"),
                                ("active", "red")]
                           )