pycharm无法打开图像文件

问题描述 投票:0回答:1

我正在尝试编写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,并且我尝试使用来运行其他人的代码,这非常相似,并且无法查明原因请帮我谢谢

python pycharm
1个回答
0
投票

我遇到了类似的问题,并且找到了解决方案。我相信这主要与虚拟环境如何运行您的应用程序有关。简而言之,它将在其他目录中运行。解决方法如下:

  1. Click on the "Edit Configuration" button.

  2. Edit the path in the highlighted box. (For my own project, I just removed the \venv path.)

我希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.