2016-06-07 45 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

但是我想回到主頁後,我點擊圖像。我問的是這樣的: 「我想要做的是當你點擊計算器圖像,它打開計算器應用程序,然後當你按下主頁按鈕,它回到家裏。「 –

相關問題