Python 无法更改Tkinter中按钮的颜色

Python 无法更改Tkinter中按钮的颜色,python,tkinter,Python,Tkinter,我无法使用fg和bg更改按钮的颜色。我得到这个错误:\u tkinter.TclError:未知选项“-fg” 发生这种情况的原因是您使用的是ttk.Button,而不是tk.Button。诸如fg、bg之类的选项不可用。相反,您必须使用样式选项,并根据需要进行配置。这里有一个例子 import tkinter as tk import tkinter.ttk as ttk root = tk.Tk() style = ttk.Style() style.configure("TButton

我无法使用fg和bg更改按钮的颜色。我得到这个错误:
\u tkinter.TclError:未知选项“-fg”


发生这种情况的原因是您使用的是
ttk.Button
,而不是
tk.Button
。诸如
fg
bg
之类的选项不可用。相反,您必须使用
样式
选项,并根据需要进行配置。这里有一个例子

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

style = ttk.Style()
style.configure("TButton", foreground="blue", background="orange")

myButton = ttk.Button(text="Scrape", style="TButton")
myButton.grid()

root.mainloop()

我可能遗漏了一些内容,但在您提供的代码中没有任何颜色设置。请提供您实际执行此操作的代码。@rbaleksandar OP很可能试图将其作为
ttk.Button
函数中的参数,但出现错误。因此删除了它,并可能呈现了正在工作的代码。我将建议修改它,这将给出一个错误。
import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

style = ttk.Style()
style.configure("TButton", foreground="blue", background="orange")

myButton = ttk.Button(text="Scrape", style="TButton")
myButton.grid()

root.mainloop()