O J*_*JOE 5 python tkinter ttk
当鼠标悬停在按钮上时,我试图将ttk.tkinter
按钮背景更改为黑色,将前景色更改为白色。已经尝试过 highlightbackground
,activebackground
但没有产生我想要的结果。
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
style = ttk.Style(root)
#style.theme_use("clam")
style.configure('TButton', foreground="black", highlightthickness=5,
highlightbackground='#3E4149', highlightforeground="white",
activebackground="black")
btr = ttk.Button(root, text="TEST BUTTON")
btr.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
小智 9
ttk 按钮外观由主题驱动(3D/Color-alt/classic/default、Color-clam)。不设置/其他按钮会使按钮呈扁平/灰色,并且设置不会改变任何内容。要使ttk TButton改变颜色可以使用map来实现。3D外观需要边框宽度。只有Classic使用高光形成外环。类似的答案请参阅:Python:根据当前颜色更改 ttk 按钮颜色?
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
style = ttk.Style()
style.theme_use("classic")
style.map("C.TButton",
foreground=[('!active', 'black'),('pressed', 'red'), ('active', 'white')],
background=[ ('!active','grey75'),('pressed', 'green'), ('active', 'black')]
)
btr = ttk.Button(root, text="TEST BUTTON", style="C.TButton")
btr.grid(column=0,row=0,sticky='nsew');
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
尝试根据您的风格使用地图函数,如下所述:
https://docs.python.org/3/library/tkinter.ttk.html
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
style = ttk.Style(root)
#style.theme_use("clam")
style.map("C.TButton",
foreground=[('pressed', 'red'), ('active', 'blue')],
background=[('pressed', '!disabled', 'black'), ('active', 'white')]
)
btr = ttk.Button(root, text="TEST BUTTON", style="C.TButton")
btr.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
使用按钮注册样式图。
我希望这有帮助。
归档时间: |
|
查看次数: |
4876 次 |
最近记录: |