简体   繁体   English

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

[英]Is there a way to stop the flashing of buttons with image when clicked in tkinter?

When creating a button with an image on it you can specify its background color to be the same as the root background color with the bg='color' parameter so that if you have an image with transparent background the result seems nice.创建带有图像的按钮时,您可以使用bg='color'参数将其背景颜色指定为与根背景颜色相同,这样如果您有一个具有透明背景的图像,结果看起来不错。 However for some reason when you click on the button, there is a white flash that covers the button for as long the click happens.但是由于某种原因,当您单击按钮时,只要单击发生,就会有一个白色的 flash 覆盖按钮。

I have this code:我有这个代码:

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

After importing PIL (if not already) and then filling the appropriate path name to your image the code should run without any problems导入 PIL(如果还没有),然后为您的图像填充适当的路径名后,代码应该可以毫无问题地运行
Notice that when you click the image a whity flash that covers the button appears?请注意,当您单击图像时,会出现覆盖按钮的白色 flash?

I want to get rid of that我想摆脱它
I've tried passing the parameters highlightcolor highlightbackground and highlightthickness and playing around with them but nothing really changes whether I for example change highlightthickness=0 or highlightbackground='blue'我尝试传递参数highlightcolor highlightbackgroundhighlightthickness并玩弄它们,但无论我是否更改highlightthickness=0highlightbackground='blue'都没有真正改变
Is there any way to do that properly?有没有办法正确地做到这一点?

Thanks in advance!提前致谢!

Oh, I just now realised the activebackground parameter does the job Everything okay now I can modify the whity flash color:P哦,我刚刚意识到activebackground参数可以完成这项工作现在一切正常我可以修改白色 flash 颜色:P

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

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