Pygame: Can't seek in this data source

2019-09-17 23:39发布

Traceback (most recent call last):
   File "G:/Computing/Project/Main.py", line 32, in <module>
    ClubsImages, SpadesImages, HeartsImages, DiamondsImages = LoadImages()
  File "G:/Computing/Project/Main.py", line 18, in LoadImages
    ClubsImages[i] = pygame.image.load(("Images/",x,".png"))
pygame.error: Can't seek in this data source

I get this error when I run this code:

def LoadImages():
    SpadesImages = {}
    ClubsImages = {}
    HeartsImages = {}
    DiamondsImages = {}
    x = 1
    for i in range (13):
        ClubsImages[i] = pygame.image.load(("Images/",x,".png"))
        x+=1
        SpadesImages[i] = pygame.image.load(("Images/",x,".png"))
        x+=1
        HeartsImages[i] = pygame.image.load(("Images/",x,".png"))
        x+=1
        DiamondsImages[i] = pygame.image.load(("Images/",x,".png"))
        x+=1
    return ClubsImages, SpadesImages, HeartsImages, DiamondsImages

I have read files like this before, except this is the first time I have tried to do it into an array and in a loop. The problem is not the loop so I have taken that out and tried it and I get an identical error. So I am thinking it's the array. I can't see any reason that you couldn't read images into an array.

标签: python pygame
1条回答
做自己的国王
2楼-- · 2019-09-18 00:29

I'm pretty sure the problem is that you're passing a tuple to pygame.image.load. According to the documentation for the function, it can either take a filename (as a string), or a file object and an optional name hint. Looking closely at your call:

pygame.image.load(("Images/", x, ".png"))

You are passing ("Images/", x, ".png"), a tuple, which could not be interpreted as a filepath. Try something like:

pygame.image.load("Images/" + str(x) + ".png") 
查看更多
登录 后发表回答