【问题标题】:Is there a way to stop the flashing of buttons with image when clicked in tkinter?在 tkinter 中单击时,有没有办法阻止带有图像的按钮闪烁?
【发布时间】:2021-04-06 18:52:10
【问题描述】:

创建带有图像的按钮时,您可以使用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=0highlightbackground='blue',都没有真正改变
有没有办法正确地做到这一点?

提前致谢!

【问题讨论】:

    标签: python tkinter tkinter-button


    【解决方案1】:

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

    【讨论】:

    • 为什么不使用tkinter.Label
    • 我只是想让白色闪光与我的背景颜色相同,所以activebackground 解决了我的问题
    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多