0

当我加载透明图像时:

def load_image(path):
    img = Image.open(path)
    return ImageTk.PhotoImage(img)

class Home_Screen(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.home = Tk()
        self.home.resizable(width = False,height = False)
        self.home.geometry("700x500+300+100")
        self.start()

    def run(self):
        self.images()
        Label(self.home, image = self.background).pack() # Put it in the display window
        button_instructions = Label(self.home,image = self.b_instructions).place(x = 300,y = 200)
        self.home.mainloop()

    def images(self):
        self.background = load_image("Images/night-sky.jpg")
        self.b_instructions = load_image("button_instructions.png")

    def start_game(self):
        self.home.destroy()

Home = Home_Screen()

我得到一个带有白色边框的图像。有谁知道为什么没有保留原来的透明度?如果可以,请您提供解决方案。

错误图片

4

1 回答 1

0

使用画布而不是标签小部件。透明度不会丢失,因为您看到的是小部件的背景。

def run(self):
    img_sky = ImageTk.PhotoImage(file="Images/night-sky.jpg")
    img_button = ImageTk.PhotoImage(file="button_instructions.png")
    self.canvas = Canvas(self.home, width=700, height=500)
    self.canvas.create_image(0, 0, image=img_sky)
    self.canvas.create_image(300, 200, image=img_button)
    self.canvas.pack()
    self.home.mainloop()
于 2013-03-26T02:14:05.933 回答