在tkinter中上传图像的麻烦

时间:2015-07-21 15:05:01

标签: python image python-3.x tkinter

我尝试使用以下代码编写tkinter应用程序的图像: [在课程的初始化功能中]

self.canvas = Canvas(self.root,
                     width = SCREEN_WIDTH,
                     height = SCREEN_HEIGHT,
                     bg = "light blue")

# The bird
bird = PhotoImage(file = 'angry_bird.png') # or gif
bird_height = self.bird_height()
self.bird = Label(self.canvas, image = bird)
self.bird.place(x = 25, y = bird_height)

# The target
target = PhotoImage(file = "pig.png") # or gif
target_coords = self.target_coordinates()
self.target = Label(self.canvas, image = target)
self.target.place(x = target_coords[0], y = target_coords[1])

当我运行代码时,图片(" pig.png"," angry_birds.png" - 我试过相同的并使用gif文件得到相同的结果)显示为灰色矩形,而不是图片。

我也尝试过那样显示图片 - 但是这里甚至没有出现灰色矩形:

    # The bird
    bird = PhotoImage(file = 'angry_bird.png') # or gif
    bird_height = self.bird_height()
    self.bird = self.canvas.create_image(25, bird_height, image = bird)

    # The target
    target = PhotoImage(file = "pig.png") # or gif
    target_coords = self.target_coordinates()
    self.target = self.canvas.create_image(target_coords[0], target_coords[1], image = target)

有什么不对吗? 还有其他方法吗?

请不要提供非内置模块解决方案

非常感谢 戴夫

0 个答案:

没有答案