在 tkinter 中单击时,有没有办法阻止带有图像的按钮闪烁?

问题描述

在创建带有图像的按钮时,您可以使用 bg='color' 参数将其背景颜色指定为与根背景颜色相同,这样如果您的图像具有透明背景,效果就会很好。 然而,由于某种原因,当您点击按钮时,只要点击发生,就会有一个白色的闪光覆盖按钮。

我有这个代码

from tkinter import Tk,Button
from PIL import Image,ImageTk

root = Tk()
root.config(bg='black')

image = Image.open('your_image_file').resize((50,50))
image = ImageTk.PhotoImage(image)

button = Button(root,width=50,height=50,image=image,bg='black')
button.pack()

root.mainloop()

在导入 PIL(如果还没有)然后将适当的路径名填充到您的图像后,代码应该可以正常运行
请注意,当您单击图像时,会出现覆盖按钮的白色闪光?

我想摆脱那个
我试过传递参数 highlightcolor highlightbackgroundhighlightthickness 并使用它们,但无论我是更改 highlightthickness=0 还是 highlightbackground='blue'
有没有办法正确地做到这一点?

提前致谢!

解决方法

哦,我刚刚意识到 activebackground 参数可以完成这项工作 现在一切正常,我可以修改白色闪光颜色:P