[英]How to change the color of ttk button
我在 Windows 上使用 Python 3.x。
我的問題是我想通過完全更改其背景和前景色來自定義ttk
的按鈕小部件。 但到目前為止,我一直沒有成功。
我想要的按鈕是:
我閱讀了ttk.Style
指南並使用了他們的代碼:
ttk.Style().configure("TButton", padding=6, relief="flat",
background="#000")
btn = ttk.Button(text="Sample")
btn.pack()
但它正在改變邊框顏色而不是整個按鈕背景。 這是輸出:
請幫我實現我想要的按鈕。
不幸的是,沒有一種簡單的方法可以從ttk
庫中更改按鈕的前景。 它始終是您圖片中的標准 Windows 灰色。
但是,如果您設置了正確的選項,您可以使用普通的tkinter.Button
輕松獲得所需的內容。 下面是一個示例腳本:
import tkinter as tk
root = tk.Tk()
btn = tk.Button(root,
bg='#000000',
fg='#b7f731',
relief='flat',
text='hello button',
width=20)
btn.pack()
root.mainloop()
這是它的樣子:
此外,我選擇的綠色只是一個例子,我認為它非常接近你想要的。 但是您可以指定您想要的任何十六進制顏色代碼。 如果您需要將 RGB 值轉換為十六進制,一個簡單的技巧是使用str.format
像這樣:
>>> rgb = (183, 247, 49)
>>> '#{:02x}{:02x}{:02x}'.format(*rgb)
'#b7f731'
>>>
雖然它不像使用 Tk 按鈕那么簡單,但它是可能的。 在 ttk 中,如果您將 theme_use 屬性設置為以下任何一個:('winnative'、'clam'、'alt'、'default'、'classic'、'vista'、'xpnative'),您應該能夠修改默認行為。 我設置了“style.map”屬性以避免由於鼠標懸停而導致的背景顏色變化(按鈕的狀態始終為“活動”)。
import tkinter as tk
from tkinter import ttk
style = ttk.Style()
style.theme_use('alt')
style.configure('TButton', background = 'red', foreground = 'white', width = 20, borderwidth=1, focusthickness=3, focuscolor='none')
style.map('TButton', background=[('active','red')])
root = tk.Tk()
button = ttk.Button(root,text='Quit')
button.place(relx=0.3,rely=0.4)
root.mainloop()
希望這可以幫助。
import ttk
root.style = ttk.Style()
#root.style.theme_use("clam")
style.configure('TButton', background='black')
style.configure('TButton', foreground='green')
button= ttk.Button(self, text="My background is black and my foreground is green.")
如果您想使用 Python 2.7 和 Tkinter 8.6 將所有按鈕更改為您“想要”的按鈕,則對我有用
簡短示例:
from tkinter import ttk
from tkinter import Tk
root = Tk()
style = ttk.Style()
button_1 = ttk.Button(root, text='click me')
style.theme_use('alt')
style.configure('TButton', font=('American typewriter', 14), background='#232323', foreground='white')
style.map('TButton', background=[('active', '#ff0000')])
button_1.pack()
root.mainloop()
長示例:
from tkinter import *
from tkinter import ttk
class App:
def __init__(self):
# Window setup
self.root = Tk()
self.root.title('BUTTONS')
WIDTH, HEIGHT = 300, 500
INITIAL_X_POSITION, INITIAL_Y_POSITION = 450, 200
self.root.geometry(f'{WIDTH}x{HEIGHT}+{INITIAL_X_POSITION}+{INITIAL_Y_POSITION}')
self.root.resizable(0, 0)
self.style = ttk.Style()
# Layout
self.button_1 = ttk.Button(self.root, text='click me', command=self.show_me_pi)
self.style.theme_use('alt')
self.style.configure('TButton', font=('American typewriter', 14), background='#232323', foreground='white')
self.style.map('TButton',
background=[('active', '#ff0000'), ('disabled', '#f0f0f0')]
)
self.button_1.pack()
self.button_2 = ttk.Button(self.root, text='click me', state='disabled')
self.button_2.pack()
self.root.mainloop()
def show_me_pi(self):
py_label = Label(self.root, text='3.14159', font=('American typewriter', 20))
py_label.pack()
app_runner = App()
import tkinter as tk
btn = tk.fButton(text="Sample", bg = "red") #Refer line 2625 in tkinter code
btn.pack()
有關更多信息,請轉到 Tkinter 代碼,請轉到第 2625 行。
在這里,您將找到問題的解決方案。
我這里使用tk.fButton
因為Tkinter的版本它doesen't支持tk.Button
,如果u與得到錯誤tk.fButton
然后用tk.Button
左寄托都將保持相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.