【问题标题】:Pygame Import error:No module named pyPygame 导入错误:没有名为 py 的模块
【发布时间】:2016-06-07 20:01:28
【问题描述】:

我正在尝试制作一个移动应用程序,您可以在其中使用 pygame 进行计算器。我想要做的是,当您单击计算器图像时,它会打开计算器应用程序,然后当您按下主页按钮时,它会返回主页。

问题:但是,如果你在打开计算器应用程序后回到主页,然后你尝试再次进入计算器应用程序,它会给我一个错误:

ImportError: 没有名为 py 的模块

但是这个错误应该不会出现,因为我已经有了 pygame 模块。

代码保存为“mytake.py”

import pygame

#Loading Images
img = pygame.image.load('main1.png')
img2= pygame.image.load('calcu.png')
img15=pygame.image.load('HOME2.png')

#Setting Screen size
w = 600
h = 450

screen = pygame.display.set_mode([w, h])
x = y = 0
running = True

pygame.init()

WHITE=(255,255,255)

#Running loop
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    screen.blit(img,(0,0))

    B=screen.blit(img2,(37,305))
    N=screen.blit(img15,(94,355))
    pygame.display.update()



    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            # Set the x, y postions of the mouse click
            #mixer.music.play(0)
            X= pygame.mouse.get_pos()
            print "Click: ",X
            print X[0],X[1]

            #Open Calculator 
            if B.collidepoint(X[0],X[1]):
                import calc.py

这是我保存为“calc.py”的计算器代码

import time
import pygame

#Loading images
img15=pygame.image.load('HOME2.png')
img16=pygame.image.load('calcimg.png')

#Screen Size
w = 600
h = 450
screen = pygame.display.set_mode([w, h])
x = y = 0
running = True

pygame.init()

WHITE=(255,255,255)
X= pygame.mouse.get_pos()
while running:


    screen.blit(img16,(0,0))
    N=screen.blit(img15,(94,359))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONUP:
            # Set the x, y postions of the mouse click
            X= pygame.mouse.get_pos()
            print "Click: ",X
            print X[0],X[1]


    #Go back to home page
    pygame.display.flip()
    if N.collidepoint(X[0],X[1]):
        import mytake.py

使用的图像使用的图像(根据给定名称保存):

另存为 caclimg

另存为计算

另存为 HOME2

另存为 main1

【问题讨论】:

    标签: python python-2.7 pygame importerror


    【解决方案1】:

    这是一个解决这个问题的尝试。

    import pygame, time, sys
    
    img = pygame.image.load('main1.png')
    img2= pygame.image.load('calcu.png')
    img15=pygame.image.load('HOME2.png')
    img16=pygame.image.load('calcimg.png')
    w = 600
    h = 450
    screen = pygame.display.set_mode([w, h])
    x=y=0
    
    pygame.init()
    WHITE=(255,255,255)
    location = 'home'
    while 1:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                X= pygame.mouse.get_pos()
                print("Click: ",X)
                print(X[0],X[1])
                if B.collidepoint(X[0],X[1]):
                    location = 'calc'
                    print(location)
                elif N.collidepoint(X[0],X[1]):
                    location = 'home'
                    print(location)
        if location == 'home':
            screen.blit(img,(0,0))
            B=screen.blit(img2,(37,305))
            N=screen.blit(img15,(94,355))
        if location == 'calc':
            screen.blit(img16,(0,0))
            N=screen.blit(img15,(94,359))
        pygame.display.update()
    

    这与pygame无关。

    简单地说,不要写import sample.py - 只使用import sample

    更多信息请参见here

    【讨论】:

    • 但是如果我再次点击计算器应用程序图像,它不会带我回到主页它只是打印鼠标点击的坐标。
    • @Krut 它不再给出错误了吗?如果是这样,那么这个问题就得到了回答。
    • 但是我想在点击图片后返回主页。我问的是:“我想要做的是当你点击计算器图片时,它会打开计算器应用程序,然后当你按下主页按钮时,它会回到主页。”
    • @Krut 好的,我现在看到了一个大问题。 imports 并不是要充当函数,所以请改用函数。只需将两个文件合二为一,设置pygame后使用功能切换屏幕即可。我有 Python 3.5,而不是 2.7,所以我不想或无法为您执行此操作,但如果您仍然遇到问题,我可以尝试指导您。
    • #StarDustGeogta ,我一直在尝试这样做,但无法理解如何将其作为一个函数。
    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 2013-05-05
    • 2018-12-08
    • 2019-01-17
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多