2016-06-07 103 views
-1

我想制作一个移动应用程序,您可以在其中使用pygame进入计算器。我试图做的是当你点击计算器图像时,它会打开计算器应用程序,然后当你按下主页按钮时,它会回到主页。Pygame导入错误:没有模块名为py

问题:不过,如果你是在家里页面打开计算器应用程序后,然后再次尝试进入计算器应用它给了我一个错误:

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 保存为caclimg
calcu
保存为calcu
HOME2
保存为HOME2
main1
保存为MAIN1

回答

3

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

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

+0

但是,如果我再次单击计算器应用程序图像,它不会带我回到主页,它只是打印鼠标点击的坐标。 –

+0

@Krut它是不是再犯错误了?如果是这样,那么这个问题就回答了。 – StardustGogeta

+0

但是我想回到主页后,我点击图像。我问的是这样的: “我想要做的是当你点击计算器图像,它打开计算器应用程序,然后当你按下主页按钮,它回到家里。“ –

相关问题