简体   繁体   中英

Cannot change color of button in Tkinter

I am not able to change the color of the button using fg and bg. I get this error: _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 . The options such as fg , bg are not supported by ttk . Instead you will have to use Style option and configure it as you require. 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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