简体   繁体   English

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

[英]Cannot change color of button in Tkinter

I am not able to change the color of the button using fg and bg. 我无法使用fg和bg更改按钮的颜色。 I get this error: _tkinter.TclError: unknown option "-fg" 我收到此错误: _tkinter.TclError: unknown option "-fg"

_scrape_btn = ttk.Button(_mainframe, text='Scrape!', command=save, fg='blue')
_scrape_btn.grid(row=2, column=0, sticky=(N,E), pady=2)

_compress_btn = ttk.Button(_mainframe, text='Compress!', command=compress)
_compress_btn.grid(row=2, column=1, sticky=W, pady=2)

The reason this is happening is because you are using ttk.Button instead of tk.Button . 发生这种情况的原因是因为您使用的是ttk.Button而不是tk.Button The options such as fg , bg are not supported by ttk . ttk不支持 fgbg类的选项。 Instead you will have to use Style option and configure it as you require. 相反,您将必须使用“ Style选项并根据需要进行配置。 Here is an example. 这是一个例子。

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()

在此处输入图片说明

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

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