![](/img/trans.png)
[英]Python: 'import node.py' raises “No module named py”-error
[英]Pygame Import error:No module named py
我正在嘗試制作一個移動應用程序,您可以在其中使用pygame進行計算器。 我想做的是,當您單擊計算器圖像時,它會打開計算器應用程序,然后在您按下主屏幕按鈕時,它會返回到主屏幕。
問題:但是,如果打開計算器應用程序后您處於主頁,然后嘗試再次轉到計算器應用程序,則會出現錯誤:
ImportError:沒有名為py的模塊
但是由於我已經有了pygame模塊,因此不會出現此錯誤。
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
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
。
有關更多信息,請參見此處 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.