Pygame Import error:No module named py

Pygame Import error:No module named py

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

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

ImportError: No module named 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

这是解决此问题的尝试。

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