简体   繁体   中英

Pygame Import error:No module named py

I am trying to make a mobile app in which you can go to calculator with pygame. What I am trying to do is when you click the calculator image, it opens calculator app and then when you press the home button , it goes back to home.

Problem: But then , if you are at home page after opening the calculator app , and then you try to again go to the calculator app it gives me an error:

ImportError: No module named py

But this error should not come since I already have the pygame module.

Code saved as '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

This is my calculator code saved as '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

Images used Images used (save according to given names):

卡climg Saved as caclimg
计算
Saved as calcu
HOME2
Saved as HOME2
主1
Saved as main1

Here is an attempt to fix this.

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()

This has nothing to do with pygame .

Simply, do not write import sample.py - use only import sample .

See here for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM