【发布时间】:2017-11-04 00:34:20
【问题描述】:
我已经想出了如何为一个按钮设置一个图像,它位于标签的顶部(我想我可能会用冗长的方式来做这件事,因为我无法在我的 Mac 上安装 PIL原因)。无论如何,它在一定程度上可以正常工作 - 我遇到的问题是它在两边都添加了空白,然后图像本身没有显示其透明背景。
我使用的代码如下:
from tkinter import *
#from PIL import Image
root = Tk()
#Removes the title bar and places over mac top bar
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
# Makes the app full screen
#root.wm_attributes('-fullscreen', 1)
root.geometry('{}x{}'.format(480, 320))
#root.attributes('-topmost', True)
def quitApp():
# mlabel = Label (root, text = 'Close').pack()
root.destroy()
background_img = PhotoImage(file="images/bg.gif")
scanBtn_img = PhotoImage(file="images/scanBtn.gif")
background = Label(root,
compound = CENTER,
quitButton = Button(image=scanBtn_img, command = quitApp).pack(),
image = background_img).pack(side="right")
background.image = background_img # keep a reference!
root.mainloop()
【问题讨论】:
-
您确定您使用的是 python 2.7,因为您将小写的 tkinter 导入为 python 3。
标签: python python-2.7 tkinter label transparency