简体   繁体   English

如何在hover it in tkinter时更改ttk按钮背景和前景

[英]How to change ttk button background and foreground when hover it in tkinter

I'm trying to change ttk.tkinter button background to black and foreground colour to white when mouse is hover it.我正在尝试将ttk.tkinter按钮背景更改为黑色,当鼠标为 hover 时将前景色更改为白色。 Have tried highlightbackground and activebackground but doesn't yield the result I'm looking for.尝试过highlightbackgroundactivebackground但没有产生我正在寻找的结果。

所需的按钮图像

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

ttk Button appearances are driven by themes (3D/Color-alt/classic/default, Color-clam). ttk 按钮外观由主题驱动(3D/Color-alt/classic/default、Color-clam)。 Not setting/others leaves buttons flat/grey and settings don't change things.不设置/其他使按钮平坦/灰色,设置不会改变事情。 To make a ttk TButton change colors can be achieved using map.要使 ttk TButton 更改颜色,可以使用 map 来实现。 3D appearance requires borderwidth.Only Classic forms an outer ring using highlight. 3D 外观需要borderwidth.Only Classic 使用highlight 形成一个外环。 Similar answer see: Python: Changing ttk button color depending on current color?类似的答案请参阅: 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()

Try using the map function with your style, as described here:尝试按照您的风格使用 map 函数,如下所述:

https://docs.python.org/3/library/tkinter.ttk.html 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()

Register the style map with the button.使用按钮注册样式映射。

I hope this helps.我希望这有帮助。

you have to try this I was having this problem before I learned this Code你必须试试这个 在我学习这个代码之前我遇到了这个问题

import tkinter 
from tkinter import ttk
from tkinter import *
import tkinter.ttk


f=Tk()

style = ttk.Style()
style.configure("BW.TLabel", foreground="blue", 
background="red")

l1 = ttk.Label(f,text="Test", style="BW.TLabel")
l2 = ttk.Label(f,text="Test", style="BW.TLabel")
l1.pack()
l2.pack()
f.mainloop(

you must see this documentation in python.org website [[it will learn you a lot of things like that I wrote 1 ] 1你必须在 python.org 网站上看到这个文档[[它会让你学到很多我写的东西1 ] 1

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

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