我收到错误:AttributeError: module 'inputbox' has no attribute 'ask'
当我试图在我的程序中打电话询问;但它是在inputbox的程序中?
这是一个输入框程序,其中ask被合并为一个函数:
import pygame, pygame.font, pygame.event, pygame.draw, string
from pygame.locals import *
def get_key():
while 1:
event = pygame.event.poll()
if event.type == KEYDOWN:
return event.key
else:
pass
def display_box(screen, message):
"Print a message in a box in the middle of the screen"
fontobject = pygame.font.Font(None,18)
pygame.draw.rect(screen, (0,0,0),
((screen.get_width() / 2) - 100,
(screen.get_height() / 2) - 10,
200,20), 0)
pygame.draw.rect(screen, (255,255,255),
((screen.get_width() / 2) - 102,
(screen.get_height() / 2) - 12,
204,24), 1)
if len(message) != 0:
screen.blit(fontobject.render(message, 1, (255,255,255)),
((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))
pygame.display.flip()
def ask(screen, question):
"ask(screen, question) -> answer"
pygame.font.init()
current_string = []
display_box(screen, question + ": " + "".join(current_string))
while 1:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
break
elif inkey <= 127:
current_string.append(chr(inkey))
display_box(screen, question + ": " + " ".join(current_string))
return "".join(current_string)
def main():
screen = pygame.display.set_mode((320,240))
print, ask(screen, "Name") + " was entered"
if __name__ == '__main__': main()
我写信时,我会在单独的程序中打电话询问:
if answer == "":
answer = inputbox.ask(screen, "Enter Integer:")
enterInt.append(answer)
这是我收到错误的地方,我无法弄清楚原因。任何帮助将不胜感激!
编辑 - 以下是我的整个代码:
#Import the necessary modules
import pygame
import sys
import inputbox
#Initialize pygame
pygame.init()
# Set the size for the surface (screen)
screenSize = (900,750)
screen = pygame.display.set_mode((screenSize),0)
# Set the caption for the screen
pygame.display.set_caption("Array")
#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)
answerFont = pygame.font.SysFont('americantypewriter', 25,True, False)
def centreHorizontal(textName):
screenWidth = screen.get_width()/2
textWidth = textName.get_width()/2
x = screenWidth - textWidth
return x
def selectedFirst():
runFirst = True
enterInt = []
while runFirst:
userInput1= ""
answer = ""
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
title = answerFont.render("Enter and Display Integers", False, NAVYBLUE)
x = centreHorizontal(title)
screen.blit(title, (x, 50))
if answer == "":
answer = inputbox.ask(screen, "Enter Integer:")
enterInt.append(answer)
print.join(answer)
resultText = "Your array is: " + str(result)
resultToScreen = answerFont.render(resultText, False, BLACK)
x = centreHorizontal(resultToScreen)
screen.blit(resultToScreen, (x,350))
pygame.display.update()
def enterInt():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def display():
runSecond = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def countInt():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def displayReverse():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def sumInt():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def average():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def findMaxORfindMin():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def search():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def writeToText():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def fillFromText():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def searchReplace():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def shuffleArray():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def wordCount():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def arrayIntro():
buttonenterInt = pygame.Rect(50, 50, 250, 100)
buttondisplay =pygame.Rect(325, 50, 250, 100)
buttoncountInt = pygame.Rect(600, 50, 250, 100)
buttondisplayReverse = pygame.Rect(50, 250, 250, 100)
buttonsumInt = pygame.Rect(325, 250, 250, 100)
buttonaverage = pygame.Rect(600, 250, 250, 100)
buttonfindMaxORfindMin = pygame.Rect(50, 450, 250, 100)
buttonsearch = pygame.Rect(325, 450, 250, 100)
buttonwriteToText= pygame.Rect(600, 450, 250, 100)
buttonfillFromText =pygame.Rect(50, 640, 250, 100)
buttonsearchReplace = pygame.Rect(325, 640, 250, 100)
buttonwordCount = pygame.Rect(600, 640, 250, 100)
intro = True
while intro:
mousePosition = pygame.mouse.get_pos()
screen.fill(MUTEDBLUE)
textChoice = pygame.font.SysFont('americantypewriter', 20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
clicked = pygame.mouse.get_pos()
if buttonenterInt.collidepoint(clicked):
selectedFirst()
elif buttondisplay.collidepoint(clicked):
display()
elif buttoncountInt.collidepoint(clicked):
countInt()
elif buttondisplayReverse.collidepoint(clicked):
displayReverse()
elif buttonsumInt.collidepoint(clicked):
sumInt()
elif buttonaverage.collidepoint(clicked):
average()
elif buttonfindMaxORfindMin.collidepoint(clicked):
findMaxORfindMin()
elif buttonsearch.collidepoint(clicked):
search()
elif buttonwriteToText.collidepoint(clicked):
writeToText()
elif buttonfillFromText.collidepoint(clicked):
fillFromText()
elif buttonsearchReplace.collidepoint(clicked):
searchReplace()
elif buttonshuffleArray.collidepoint(clicked):
shuffleArray()
elif buttonwordCount.collidepoint(clicked):
wordCount()
#########################################################################################
#Top Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))
enterInt = textChoice.render("enterInt", True, NAVYBLUE)
screen.blit(enterInt, (135, 90))
if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))
display = textChoice.render("display", True, NAVYBLUE)
screen.blit(display, (414, 90))
if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))
countInt = textChoice.render("countInt", True, NAVYBLUE)
screen.blit(countInt, (680, 90))
#########################################################################################
#Middle Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))
displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
screen.blit(displayReverse, (102, 287))
if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))
sumInt = textChoice.render("sumInt", True, NAVYBLUE)
screen.blit(sumInt, (410, 287))
if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))
average = textChoice.render("average", True, NAVYBLUE)
screen.blit(average, (685, 287))
#########################################################################################
#Middle Bottom Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))
findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
screen.blit(findMaxORfindMin, (95, 488))
if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))
search = textChoice.render("search", True, NAVYBLUE)
screen.blit(search, (417, 488))
if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))
writeToText = textChoice.render("writeToText", True, NAVYBLUE)
screen.blit(writeToText, (665, 488))
#########################################################################################
#Bottom Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))
fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
screen.blit(fillFromText, (115, 680))
if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))
searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
screen.blit(searchReplace, (382, 680))
if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))
wordCount = textChoice.render("wordCount", True, NAVYBLUE)
screen.blit(wordCount, (670, 680))
pygame.display.update()
#########################################################################################
eval(selected)
arrayIntro()