這是我做的代碼:它是會說這是JS,但它的Python不打印什麼,我需要它太(Python)的
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()
,我得到的是錯誤:
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\stickfiguregame.py", line 34, in <module>
g = Game()
File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\stickfiguregame.py", line 16, in __init__
self.bg = PhotoImage(file = "background.gif")
File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "background.gif": no such file or directory
我有一張名爲background的圖片文件,它是.gif。 爲什麼它不起作用? 僅供參考我正在使用Python 3.6.1
是你的圖像存儲在你的腳本相同的目錄? – DevB2F
我是一個相對較新的Python初學者,所以你是什麼意思? –
我的意思是你必須確保python文件與你的gif圖像位於同一個文件夾中。例如,如果您的python文件在您的桌面上,請確保gif圖像也位於桌面上。 – DevB2F