Pygame with Multiple Windows

14,257

Solution 1

The short answer is no, creating two pygame windows in the same process is not possible (as of April 2015). If you want to run two windows with one process, you should look into pyglet or cocos2d.

An alternative, if you must use pygame, is to use inter-process communication. You can have two processes, each with a window. They will relay messages to each other using sockets. If you want to go this route, check out the socket tutorial here.

Solution 2

Internally set_mode() probably sets a pointer that represents the memory of a unique display. So if we write:

screenA = pygame.display.set_mode((500,480), 0, 32)
screenB = pygame.display.set_mode((500,480), 0, 32)

For instance we can do something like that later:

screenA.blit(background, (0,0))
screenB.blit(player, (100,100))

both blit() calls will blit on the same surface. screenA and screenB are pointing to the same memory address. Working with 2 windows is quite hard to achieve in pygame.

Share:
14,257

Related videos on Youtube

Marjoram
Author by

Marjoram

Python programmer.

Updated on June 22, 2022

Comments

  • Marjoram
    Marjoram almost 2 years

    I need to to build an application that has multiple windows. In one of these windows, I need to be able to play a simple game and another window has to display questions and get response from a user that influences the game.

    (1) I was wanting to use pygame in order to make the game. Is there a simple way to have pygame operate with multiple windows?

    (2) If there is no easy way to solve (1), is there a simple way to use some other python GUI structure that would allow for me to run pygame and another window simultaneously?

  • dmitry_romanov
    dmitry_romanov about 8 years
    Sockets might be too low-level solution. ZeroMq or other similar library may be enough to push messages back and forth.
  • Darrel Lee
    Darrel Lee about 2 years
    You can open multiple windows and control them from a single event loop. The drawing method you use is different and you have to track WINDOWENTER and WINDOWLEAVE events so you know where to send the input events.