Pygame导入错误:没有名为py的模块

时间:2016-06-07 20:01:29

标签: python python-2.7 pygame importerror

我正在尝试制作一款移动应用,您可以使用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 保存为caclimg
calcu
保存为计算 HOME2
保存为HOME2
main1
保存为main1

1 个答案:

答案 0 :(得分:3)

这是尝试解决此问题。

@media yahoo {min-width:0!important}

这与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

有关详细信息,请参阅here