使用ImageTk时我的图像不透明

问题描述

我正在尝试将图像放入带有ImageTkPhotoImage的窗口中。下面是代码

import tkinter as tk
import random as r
from PIL import ImageTk,Image


window = tk.Tk()

HEIGHT = window.winfo_screenwidth()
WIDTH = window.winfo_screenheight()
RESOL = str(str(HEIGHT) + "x" + str(WIDTH+7) + "+" + str(-7) + "+" + str(0))
window.geometry(RESOL)

x = int(HEIGHT)/2
y = int(WIDTH)/2
updatex = HEIGHT + (0.6 * HEIGHT)

main = tk.Frame(window,height = WIDTH,width = HEIGHT)
main.configure(bg = "white",highlightthickness = 0)
main.place(x = x,y = y,anchor = "center")

Map = tk.Canvas(window,height = int((900 - int(x))) + int(900),width = int((900 - int(y))) + int(900),bg = "#44b863",highlightthickness = 0)
Map.place(x = updatex,y = int(y),anchor = "center")

p = tk.PhotoImage(file = "TitleForGameReal.png")
play_solo_image = tk.PhotoImage(file = "PlaySoloButton.png")
play_duo_image = tk.PhotoImage(file = "PlayDuoButton.png")

title = tk.Label(main,image = p,highlightthickness = 0,bd = 0)
title.place(relx = 0.5,rely = 0.35,anchor = "center")

class CustomButton:
    def __init__(self,image,master,height,width,bordercolor):
        self.master = master
        self.frame = tk.Frame(master,height = height,width = width,highlightthickness = 0)
        self.image = tk.Label(self.frame,image = image,borderwidth = 0,bg = "dark grey")
        self.bordercolor = bordercolor
    def put(self,x,y,command):
        self.x,self.y = x,y
        self.frame.place(relx = x,rely = y,anchor = "center")
        self.image.pack()

        def enter(event = "<Enter>"):
            self.image.config(borderwidth = 3)
        self.image.bind("<Enter>",enter)

        def leave(event = "<Leave>"):
            self.image.config(borderwidth = 0)
        self.image.bind("<Leave>",leave)
        def bind_command(event = "<Button-1>"):
            command()
        self.image.bind("<Button -1>",bind_command)


def solo():
    global x,updatex
    for i in range(int(int(int(HEIGHT/9))/2)):
        x -= 20
        updatex -= 20
        main.place(x = x,anchor = "center")
        Map.place(x = updatex,anchor = "center")
        main.update()
        Map.update()
    player_image = Image.open("Connector.png")
    player_image2 = ImageTk.PhotoImage(player_image)
    
    class Player:
        def __init__(self,hp,image):
            self.hp = hp
            self.image = tk.Label(Map,image = image)
            
            self.image.place(relx = 0.5,rely = 0.5,anchor = "center")
        def spawn(self):
            Map.create_image(updatex,image = self.image)
    player = Player(100,player_image2)
    player.spawn()

def duo():
    print("duo")

play_solo_image = tk.PhotoImage(file = "PlaySoloButton.png")
play_solo_button = CustomButton(image = play_solo_image,master = main,height = play_solo_image.height(),width = play_solo_image.width(),bordercolor = "grey")
play_solo_button.put(x = 0.39,y = 0.47,command = solo)

play_duo_button = CustomButton(image = play_duo_image,height = play_duo_image.height(),width = play_duo_image.width(),bordercolor = "grey")
play_duo_button.put(x = 0.61,command = duo)

我还将图像作为参考,因为我编辑了照片以使其在PhotoShop中严格具有透明背景:

enter image description here

但这是我看到的输出

enter image description here

后来我意识到我也收到了一个错误,该错误可能与我的问题有关:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\offcampus\AppData\Local\Programs\Python\python35\lib\idlelib\run.py",line 125,in main
    seq,request = rpc.request_queue.get(block=True,timeout=0.05)
  File "C:\Users\offcampus\AppData\Local\Programs\Python\python35\lib\queue.py",line 172,in get
    raise Empty
queue.Empty

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\offcampus\AppData\Local\Programs\Python\python35\lib\tkinter\__init__.py",line 1559,in __call__
    return self.func(*args)
  File "C:\Users\offcampus\Desktop\New folder\SurvivalGame.py",line 50,in bind_command
    command()
  File "C:\Users\offcampus\Desktop\New folder\SurvivalGame.py",line 75,in solo
    player.spawn()
  File "C:\Users\offcampus\Desktop\New folder\SurvivalGame.py",line 73,in spawn
    Map.create_image(updatex,image = self.image)
  File "C:\Users\offcampus\AppData\Local\Programs\Python\python35\lib\tkinter\__init__.py",line 2338,in create_image
    return self._create('image',args,kw)
  File "C:\Users\offcampus\AppData\Local\Programs\Python\python35\lib\tkinter\__init__.py",line 2329,in _create
    *(args + self._options(cnf,kw))))
_tkinter.TclError: image ".2469091717696.2469132063968" doesn't exist

我知道这可能与垃圾回收有关,我通常会保存它的引用,但是问题是我不能这样做,因为它会告诉我“您不能打包PhotoImage”。

This是我用来使图像背景透明的教程。这不是我的视频,所以不要将其标记垃圾邮件

解决方法

问题是image的{​​{1}}选项需要一个PhotoImage实例,但是您要给它加上标签,因此只需说一下即可解决。

Map.create_image(updatex,y,image = self.image)

希望这可以解决错误,请让我知道是否有任何错误或疑问。