Python 点击按钮多次调用函数?

Python 点击按钮多次调用函数?,python,function,button,pygame,Python,Function,Button,Pygame,我的python/Pygame程序以一个调用函数的按钮和一个退出游戏的按钮开始。我想按下第一个按钮,然后一个函数被调用一次,然后,它应该返回到开始按钮屏幕,让我通过点击按钮再次调用该函数。我该怎么做?目前我只能点击按钮,调用函数,然后游戏结束。在下面的代码中,您可以看到代码中最重要的部分 def function(): .... def main(): screen = pygame.display.set_mode((640, 480)) clock = pygame.

我的python/Pygame程序以一个调用函数的按钮和一个退出游戏的按钮开始。我想按下第一个按钮,然后一个函数被调用一次,然后,它应该返回到开始按钮屏幕,让我通过点击按钮再次调用该函数。我该怎么做?目前我只能点击按钮,调用函数,然后游戏结束。在下面的代码中,您可以看到代码中最重要的部分

 def function():
 ....

 def main():
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    done = False

 def quit_game():  # A callback function for the button.
        nonlocal done
        done = True

    button1 = create_button(100, 100, 250, 80, 'function', function)
    button2 = create_button(100, 200, 250, 80, 'quit', quit_game)
        # A list that contains all buttons.
        button_list = [button1, button2]

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                # This block is executed once for each MOUSEBUTTONDOWN event.
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    # 1 is the left mouse button, 2 is middle, 3 is right.
                    if event.button == 1:
                        for button in button_list:
                            # `event.pos` is the mouse position.
                            if button['rect'].collidepoint(event.pos):
                                # Increment the number by calling the callback
                                # function in the button list.
                                button['callback']()


            screen.fill(WHITE)
            for button in button_list:
                draw_button(button, screen)
            pygame.display.update()
            clock.tick(30)


    main()

这里您需要的是将每个屏幕/游戏循环隔离为其自己的特殊功能:

因此,对于我的按钮屏幕,我可以制作如下功能:

def title():
    button1 = create_button(100, 100, 250, 80, 'game', game) # This will call the game function later in the file
    button2 = create_button(100, 200, 250, 80, 'quit_game', quit_game)
    # A list that contains all buttons.
    button_list = [button1, button2]
    # And so on with the rest of the code...
对于主游戏,您可以执行相同的操作:

def game():
    button1 = create_button(100, 100, 250, 80, 'Exit', title) # This button will return you to the title
    # And whatever else you need     
之后,在文件末尾,您可以添加以下内容:

if __name__ == '__main__':
    pygame.init()
    title() # Call the title first, and then further functions
    pygame.quit()
您必须注意,当您激活按钮回调时,需要一个
返回
,然后才能卸载该屏幕,否则您将只是将游戏循环层叠在一起

因此,在事件循环期间:

if event.button == 1:
    for button in button_list:
        # `event.pos` is the mouse position.
        if button['rect'].collidepoint(event.pos):
            # Increment the number by calling the callback
            # function in the button list.
            button['callback']()
            return # Leave the function now that the new one is called.

我不能确切地告诉你这里想要什么,当你点击一个按钮时,你想调用一个函数几次吗?你想要一个按钮,当点击多次退出游戏吗?你想用按钮开始游戏吗?我不太明白你在说什么。对不起,我的英语不是最好的。多次我的意思是:我想单击按钮,它调用函数一次,然后返回到按钮屏幕,给我再次单击的机会。从最内部的for循环中,您实际上调用两个按钮回调,因此在执行button1函数后,它将调用
退出游戏
,这是button2回调。不确定你是不是在问这个问题。好吧,那么当你点击这个按钮时,你想进入游戏,然后可以回到主屏幕@ddd@Pygasm,是的,这就是我要找的。你能不能弄清楚“报税表”需要写在哪里?