所以我的程序是按行显示按钮,然后等待用户点击一个,但是会发生什么,它会返回主菜单。
我不确定如何使程序等待(我尝试了time.sleep()和os.system("暂停"),但是这两个都只是等待一定的时间和然后还返回主菜单 - 所以我使用控制台输入,只是为了看看盒子是否正确显示。
然而,现在使用控制台输入,单击任何按钮会使程序无响应(当按钮应该改变颜色时) - 我已经包含了一个屏幕截图。 Program not responding
我想知道自己做错了什么,我知道这有点乱,我对某种回应感到非常高兴,如果需要,我可以提供更多信息。 非常感谢。
这是显示按钮的代码。
screen.fill(white) #clear the screen
xPos = -135;yPos = 200;tasktodo = "null" #set the position of where the first buttons should be placed
for i in tasklist[posInList][2]: #For each task the user has been set
for y in QuestionTypes: #For each item in QuestionTypes ([MPQ,DBQ,RatioQ]...)
if str(y) in str(i): #If the any item in QuestionTypes is in the tasks the user is set:
task = (i.split(",")[0]) #This splits up the various tasks to the form of [DBQ,MPQ,AFQ]
if xPos > display_height: #If the place a button would be placed is out of bounds
xPos = 50 #Reset xPos
yPos += yPos -82 #Lower the position of the button to another row
else:
xPos += 185 #Else, place the button 185 pixels to the right
button(str(task), xPos, yPos, 150, 90, grey, lightGrey, action=str(task)) #Place a button with the name of the task, in position (xPos,yPos), dimensions (150,90),active/inactive colors of grey/light grey action being the task
listOfSetQuestions.append(y) #Append the tasks the user has to do to a list
amountoftasks += 1 #Keep track of the amount of tasks the user has to do.
# After the tasks are all printed, the user must input what task they wish to do
message_to_screen("These are the tasks your teacher has set, pick one:", black, "medium", -200) #send a message to the screen, black color, medium size font and y position of -200
pygame.display.update()
x = input("press ENTER to continue")
这是按钮的代码,因此您可以查看更多上下文。
def button(text, x, y, width, height, inactiveColor, activeColor, action = None):
cur = pygame.mouse.get_pos() #Gets the position of the cursor
click = pygame.mouse.get_pressed() #will return a tuple of 3, click[0] is left click, click[1] is middle and click[2] is right.
if x + width > cur[0] > x and y + height > cur[1] > y: #If the mouse is clicked and is between the borders of the button.
pygame.draw.rect(screen, activeColor, (x,y,width,height)) #Change the color of the button to the active color, to show it is clicked
if click[0] == 1 and action != None: #if the mouse is clicked, and there is a purpose to the click (clicked on a button)
if action == "Create":
Create()
if action == "Sign in":
Sign_In()
if action == "Teacher":
pass
if action in QuestionTypes:
print (action)
global tasktodo
tasktodo = action
else:
pygame.draw.rect(screen, inactiveColor, (x,y,width,height))
text_to_button(text,black,x,y,width,height)
答案 0 :(得分:1)
当要求input
时,程序停止并等到用户输入内容。当程序停止时,它无法处理使你的操作系统相信程序崩溃的事件。因此,当使用pygame时,您无法获得控制台输入(除非您创建进程)。
如果你需要的只是延迟,你可以使用while
- 循环
def wait(time):
clock = pygame.time.Clock()
while time > 0:
dt = clock.tick(30) / 1000 # Takes the time between each loop and convert to seconds.
time -= dt
pygame.event.pump() # Let's pygame handle internal actions so the os don't think it has crashed.
如果您希望用户决定等待多长时间,您可以检查用户事件
def wait_user_response():
clock = pygame.time.Clock()
waiting = True
while waiting:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN: # Or whatever event you're waiting for.
waiting = False
当然还有其他更好的方法,但由于我不知道你的程序如何运作,这就是我所能提供的。