图像显示问题

时间:2013-03-26 00:33:45

标签: python image tkinter transparency python-imaging-library

当我用:

加载透明图像时
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()

我得到一张周围有白色边框的图片。有谁知道为什么没有保留原始的透明度?如果是这样,请提供解决方案。

Image of error

1 个答案:

答案 0 :(得分:0)

使用Canvas而不是Label小部件。透明度不会丢失,因为您看到的是窗口小部件的背景。

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()