Python 如何在tkinter ttk.button中使用不同的颜色为按钮添加更多颜色?

Python 如何在tkinter ttk.button中使用不同的颜色为按钮添加更多颜色?,python,button,tkinter,colors,Python,Button,Tkinter,Colors,我想给按钮上色,但目前我只能一次给所有的按钮上色 我在ttk.button()中尝试了bg=“red”,但它返回了以下错误: /\ukinter.TclError:未知选项“-bg” 希望您能帮助我,我将不胜感激。您不能在ttk.Style()中使用背景和前景的bg或fg缩写形式。您必须使用完整的单词background和foreground来配置样式 tkinter.TclError:未知选项“-bg” 您遇到的错误是因为无法将-bg作为参数传递给ttk.Button()。要配置任何ttk小部

我想给按钮上色,但目前我只能一次给所有的按钮上色

我在
ttk.button()
中尝试了
bg=“red”
,但它返回了以下错误:

/\ukinter.TclError:未知选项“-bg”

希望您能帮助我,我将不胜感激。

您不能在
ttk.Style()中使用背景和前景的bg或fg缩写形式。
您必须使用完整的单词background和foreground来配置样式

tkinter.TclError:未知选项“-bg”

您遇到的错误是因为无法将-bg作为参数传递给
ttk.Button()
。要配置任何ttk小部件的样式,您必须使用
ttk.style
及其受人尊敬的样式名称,如按钮:“TButton”、标签:“TLabel”、框架:“TFrame”等,请参见Tk主题小部件的说明

若要为不同的按钮创建单独的样式,则可以创建自定义样式名称

例如:

from tkinter import *
from tkinter import ttk

root = Tk()

button1_style = ttk.Style() # style for button1
# Configure the style of the button here (foreground, background, font, ..)
button1_style.configure('B1.TButton', foreground='red', background='blue')
button1 = ttk.Button(text='Deletar', style='B1.TButton')
button1.pack()

button2_style = ttk.Style() # style for button2
# Configure the style of the button here (foreground, background, font, ..)
button2_style.configure('B2.TButton', foreground='blue', background='red')
button2 = ttk.Button(text='Editar', style='B2.TButton')
button2.pack()

root.mainloop()
from tkinter import *
from tkinter import ttk

root = Tk()

button1_style = ttk.Style() # style for button1
# Configure the style of the button here (foreground, background, font, ..)
button1_style.configure('B1.TButton', foreground='red', background='blue')
button1 = ttk.Button(text='Deletar', style='B1.TButton')
button1.pack()

button2_style = ttk.Style() # style for button2
# Configure the style of the button here (foreground, background, font, ..)
button2_style.configure('B2.TButton', foreground='blue', background='red')
button2 = ttk.Button(text='Editar', style='B2.TButton')
button2.pack()

root.mainloop()