我正在尝试编写python计算器的代码,这是我的代码
main.py:
import pygame
import os
import Item
global items
one = Item.Item('/images/numbers/1.png', "1", 200, 200) # here is where you specify the image file name ( must be exact!)
two = Item.Item('/images/numbers/2.png', "2", 200, 200) # here is where you specify the image file name ( must be exact!)
three = Item.Item('/images/numbers/3.png', "3", 200, 200) # here is where you specify the image file name ( must be exact!)
four = Item.Item('/images/numbers/4.png', "4", 200, 200) # here is where you specify the image file name ( must be exact!)
five = Item.Item('/images/numbers/5.png', "5", 200, 200) # here is where you specify the image file name ( must be exact!)
six = Item.Item('/images/numbers/6.png', "6", 200, 200) # here is where yo specify the image file name ( must be exact!)
seven = Item.Item('/images/numbers/7.png', "7", 200, 200) # here is where you specify the image file name ( must be exact!)
eight = Item.Item('/images/numbers/8.jpg', "8", 200, 200)# here is where you specify the image file name ( must be exact!)
nine = Item.Item('/images/numbers/9.png', "9", 200, 200)# here is where you specify the image file name ( must be exact!)
ten = Item.Item('/images/numbers/0.jpg', "0", 200, 200)# here is where you specify the image file name ( must be exact!)
plus = Item.Item('/images/sim/+.png', "+", 200, 200)# here is where you specify the image file name ( must be exact!)
minus = Item.Item('/images/sim/-.png', "-", 200, 200
这是item.py:
import pygame
import os
global items
items = pygame.sprite.Group()
class Item(pygame.sprite.Sprite):
global hide
# the constructor
def __init__(self, img_name, name, x, y):
pygame.sprite.Sprite.__init__(self, items)
self.image = pygame.image.load(os.path.join(img_name))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.name = name;
self.hide = False
# to display on game panel
def display(self, surface):
if self.hide == False:
surface.blit(self.image, self.rect)
但是当我在pycharm上运行它时,它是这样的:
/usr/local/bin/python3.7 /Users/oscar/Desktop/python_calculator/main.py
pygame 1.9.6
Traceback (most recent call last):
Hello from the pygame community. https://www.pygame.org/contribute.html
File "/Users/oscar/Desktop/python_calculator/main.py", line 8, in <module>
one = Item.Item('/images/numbers/1.png', "1", 200, 200) # here is where you specify the image file name ( must be exact!)
File "/Users/oscar/Desktop/python_calculator/Item.py", line 14, in __init__
self.image = pygame.image.load(os.path.join(img_name))
pygame.error: Couldn't open /images/numbers/1.png
Process finished with exit code 1
我有pygame下载,我尝试运行与他人的代码非常相似,但无法查明原因 请帮我 谢谢
答案 0 :(得分:0)
我遇到了类似的问题,并且找到了解决方案。 我相信这主要与虚拟环境如何运行您的应用程序有关。简而言之,它将在其他目录中运行。解决方法如下:
我希望这会有所帮助!
答案 1 :(得分:0)
将图像与要使用的代码插入同一项目文件中。希望对您有帮助
答案 2 :(得分:-2)
由于python仅支持文件的.gif扩展名,因此请更改文件格式。