python tkinter button 透明图片_Tkinter create_image()保留PNG透明度,但Button(image)不保留...

您可以创建从canvas继承的自定义按钮类,并像使用Button()一样使用它。我给你做了一个希望你觉得有用。在

892d3e2b0dce9e685cb85304732b9833.png

自定义按钮类:

将此类另存为imgbutton.py,然后将其导入主文件。还要确保它与主文件所在的目录相同。或者您可以在导入后将其保存在主文件的顶部。在import tkinter as tk

class Imgbutton(tk.Canvas):

def __init__(self, master=None, image=None, command=None, **kw):

# Declared style to keep a reference to the original relief

style = kw.get("relief", "groove")

if not kw.get('width') and image:

kw['width'] = image.width()

else: kw['width'] = 50

if not kw.get('height') and image:

kw['height'] = image.height()

else: kw['height'] = 24

kw['relief'] = style

kw['borderwidth'] = kw.get('borderwidth', 2)

kw['highlightthickness'] = kw.get('highlightthickness',0)

super(Imgbutton, self).__init__(master=master, **kw)

self.set_img = self.create_image(kw['borderwidth'], kw['borderwidth'],

anchor='nw', image=image)

self.bind_class( self, '',

lambda _: self.config(relief='sunken'), add="+")

# Used the relief reference (style) to change back to original relief.

self.bind_class( self, '',

lambda _: self.config(relief=style), add='+')

self.bind_class( self, '',

lambda _: command() if command else None, add="+")

下面是一个如何使用它的示例

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值