Tkinter create_image() 保留 PNG 透明度,但 Button(image)

TkVersion = 8.6,Python 版本 3.7.3


我正在尝试使用 PNG 图像在 python 中创建一个带有 tkinter 的按钮。图像的透明角是否透明取决于我使用的小部件。它似乎canvas.create_image是唯一保留透明度的小部件。


首先,我使用在画布上添加图像create_image(0,0, image=button)并且效果很好 - 圆角是透明的。


但是当我尝试将它实现为使用Button()和create_window()小部件的实际按钮时,角落被白色填充。


button = ImageTk.PhotoImage(file="button.png")


canvas = tk.Canvas(width=200, heigh=200, borderwidth=0, highlightthickness=0)

canvas.grid()

canvas.create_rectangle(0,0,199,199, fill="blue")


canvas.create_image(0,0, image=button, anchor="nw")

http://img4.mukewang.com/61e6761300018edf02000219.jpg

button = ImageTk.PhotoImage(file="button.png")


canvas = tk.Canvas(width=200, heigh=200, borderwidth=0, highlightthickness=0)

canvas.grid()

canvas.create_rectangle(0,0,199,199, fill="blue")


buttonWidget = tk.Button(root, image=button)

canvas.create_window(0,0, window=buttonWidget, anchor="nw")

http://img3.mukewang.com/61e6761f0001b71a01980218.jpg

如何使 PNG 按钮角透明?

这也是按钮图像:

http://img.mukewang.com/61e6762a000132f701820085.jpg

largeQ
浏览 388回答 1
1回答

慕雪6442864

你可以让自己的自定义按钮类继承自画布,并像使用Button().&nbsp;我为你做了一个,希望对你有帮助。自定义按钮类:将此类保存在单独的文件中imgbutton.py,然后将其导入主文件。还要确保它与主文件位于同一目录中。或者您可以在导入后将其保留在主文件的顶部。import tkinter as tkclass Imgbutton(tk.Canvas):&nbsp; &nbsp; def __init__(self, master=None, image=None, command=None, **kw):&nbsp; &nbsp; &nbsp; &nbsp; # Declared style to keep a reference to the original relief&nbsp; &nbsp; &nbsp; &nbsp; style = kw.get("relief", "groove")&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if not kw.get('width') and image:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kw['width'] = image.width()&nbsp; &nbsp; &nbsp; &nbsp; else: kw['width'] = 50&nbsp; &nbsp; &nbsp; &nbsp; if not kw.get('height') and image:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kw['height'] = image.height()&nbsp; &nbsp; &nbsp; &nbsp; else: kw['height'] = 24&nbsp; &nbsp; &nbsp; &nbsp; kw['relief'] = style&nbsp; &nbsp; &nbsp; &nbsp; kw['borderwidth'] = kw.get('borderwidth', 2)&nbsp; &nbsp; &nbsp; &nbsp; kw['highlightthickness'] = kw.get('highlightthickness',0)&nbsp; &nbsp; &nbsp; &nbsp; super(Imgbutton, self).__init__(master=master, **kw)&nbsp; &nbsp; &nbsp; &nbsp; self.set_img = self.create_image(kw['borderwidth'], kw['borderwidth'],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anchor='nw', image=image)&nbsp; &nbsp; &nbsp; &nbsp; self.bind_class( self, '<Button-1>',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lambda _: self.config(relief='sunken'), add="+")&nbsp; &nbsp; &nbsp; &nbsp; # Used the relief reference (style) to change back to original relief.&nbsp; &nbsp; &nbsp; &nbsp; self.bind_class( self, '<ButtonRelease-1>',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lambda _: self.config(relief=style), add='+')&nbsp; &nbsp; &nbsp; &nbsp; self.bind_class( self, '<Button-1>',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lambda _: command() if command else None, add="+")这是一个如何使用它的示例import tkinter as tkfrom imgbutton import Imgbutton&nbsp; &nbsp; # Import the custom button classroot = tk.Tk()root['bg'] = 'blue'but_img = tk.PhotoImage(file='button.png')but = Imgbutton(root, image=but_img, bg='blue',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command=lambda: print("Button Image"))but.pack(pady=10)root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python