'Where to put closing for Pygame

I've made a game using Pygame module and Tkinter module using mostly messagebox part. I don't know where should I put code for closing Pygame window because I can't close the window.

for e in event.get():
    if e.type == QUIT:
        pygame_quit()
        sys_exit()

This is the code where I need to put it:

def main(playing=True):
    turn = 0
    characters = sorted(['cruise', 'car', 'cap', 'derby', 'cat', 'cloud', 'tractor', 'tomato'])
    for index in range(1, 5):
        try:
            characters.remove(settings[f'player_{index}_character'])
        except ValueError:
            pass
    players_count = settings['players_number']
    if players_count == 0:
        players_count = 2

    for player_index in range(1, players_count + 1):
        with open('resources/players.json', 'r') as f:
            players_info = load(f)
        player_name = settings[f'player_{player_index}_username']
        player_character = settings[f'player_{player_index}_character']
        if player_name == '':
            player_name = f'guest_{player_index}'
        if player_character == '':
            player_character = choice(characters)
            characters.remove(player_character)
        players_info['username'] = player_name
        players_info['character'] = player_character
        players[f'player_{player_index}'] = players_info

    PygameClass.players_start()

    while playing:
        for e in event.get():
            if e.type == QUIT:
                pygame_quit()
                sys_exit()
        turn += 1
        if turn > players_count:
            turn = 1
        player_name = players[f'player_{turn}']['username']
        player_character = players[f'player_{turn}']['character']
        messagebox.showinfo('Monopoly', f'It\'s {player_name}\'s turn. ({player_character.upper()})')
        if players[f'player_{turn}']['in_jail']:
            messagebox.showinfo('Monopoly', 'You paid $50. You are out of Jail!')
            players[f'player_{turn}']['in_jail'] = False
            players[f'player_{turn}']['money'] -= 50
            players[f'player_{turn}']['plot_index'] = 10
            PygameClass.move_player(turn)
            PygameClass.money.play()
        else:
            move(dice(), turn)

main()
mainloop()

In the code calling move() function many more code is getting executed tho.



Solution 1:[1]

You are using pygame loop wrong.

def main():
  # something

while playing:
  for e in event.get():
    if e.type == QUIT:
      pygame_quit()
      playing = False
  main()

Mixing tkinter and pygame is a bad idea, pygame is a great module for making games and it's not that hard to learn so spend a bit more time learning it instead of mixing it with tkinter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 miaa26