[英]Cannot change color of button in Tkinter
我無法使用fg和bg更改按鈕的顏色。 我收到此錯誤: _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)
發生這種情況的原因是因為您使用的是ttk.Button
而不是tk.Button
。 ttk不支持 fg
, bg
類的選項。 相反,您將必須使用“ Style
選項並根據需要進行配置。 這是一個例子。
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.