有没有办法在 python 中更改 ttk 按钮的背景颜色?我尝试使用样式方法,但它只是改变了边框颜色


from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
root = Tk()
stl = ttk.Style()
stl.map('C.TButton',
foreground = [('pressed','red'),('active','blue')],
background = [('pressed','!disabled','black'),('active','white')]
)
#background not changing.It is still grey
ttk.Button(root, text='This is a button', style='C.TButton').pack()
root.mainloop()

我尝试使用样式类并在 C.TButton 中进行了一些更改,但似乎它只是更改边框颜色而不是更改按钮的颜色。按钮仍然是灰色和扁平的帮助!

我也遇到了同样的问题。 您可以通过使用 TLabel 而不是 TButton 来更改所需的背景颜色。 但是,按钮级别文本周围没有填充空间。您需要使用配置方法指定填充。 如果指定 TLabel,则作为按钮的样式似乎丢失,并且还需要指定浮雕。

from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
root = Tk()
stl = ttk.Style()
root.geometry('800x600')
stl = ttk.Style()
stl.configure('C.TLabel',padding=[30,10,50,60])
stl.map('C.TLabel',
foreground = [('pressed','red'),('active','blue')],
background = [('pressed','!disabled','black'),('active','white')],
relief=[('pressed', 'sunken'),
('!pressed', 'raised')]
)
ttk.Button(root, text='This is a button', style='C.TLabel').pack()
root.mainloop()

最新更新