如何让我的图像被闲置的python识别

2024-09-29 19:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个编程语言的初学者,这是我第一次使用GIMP应用程序,这是我第二次通过Python制作一个简单的游戏。所以我仍然是一个阿马图尔和我有一个问题。我通过GIMP应用程序制作了background.gif。我将它保存为“gif”扩展名,即使我在GIMP中没有看到在该程序中将其设置为gif的任何选项,更糟糕的是,我只需单击与教程“外观”相同的内容。因为我在教程中看到说背景必须是gif扩展名,然后我把它变成了gif。然后当我用空闲的python运行我的文件时,它说我的背景无法识别。GIMP应用程序上的backround.gif或我的代码是否有问题。虽然我的代码和教程代码是一样的,但它们都是一样的

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Acer\Desktop\Stickman\stickmangame.py", line 35, in <module>
    g = Game()
  File "C:\Users\Acer\Desktop\Stickman\stickmangame.py", line 16, in __init__
    self.bg = PhotoImage(file="background.gif")
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3545, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3501, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "background.gif"

这是我的密码:

from tkinter import *
import random
import time

class Game:
    def __init__(self):
        self.tk = Tk()
        self.tk.title("Mr. Stick Man Races For The Exit")
        self.tk.resizable(0,0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=500, height=500, highlightthickness=0)
        self.canvas.pack()
        self.tk.update()
        self.canvas_height = 500
        self.canvas_width = 500
        self.bg = PhotoImage(file="background.gif")
        w = self.bg.width()
        h = self.bg.height()
        for x in range(0, 5):
            for y in range(0, 5):
                self.canvas.create_image(x * w, y * h, image=self.bg, anchor='nw')
        self.sprites = []
        self.running = True

    def mainloop(self):
        while 1:
            if self.running == True:
                for sprite in self.sprites:
                    sprite.move()
            self.tk.update_idletasks()
            self.tk.update()
            time.sleep(0.01)


g = Game()
g.mainloop()

它们都与教程代码相同。我检查了很多次

我希望画布会显示我的背景.gif

这是我在GIMP应用程序中找到的选项: gimp extentsion available

然后单击GIMP XCF图像。并显示所有文件。保存它。如果不是那样的话,我就不能把它保存为GIF扩展名


Tags: 代码inself应用程序init教程gifusers

热门问题