2

In my program, I'm trying to have an input right after the display screen is updated in pygame. I noticed for some reason the screen is only updated AFTER the user enters in an input, although the input function is after pygame.display.update(). Why does this happen, and how can the code be fixed?

# python 3.6.5
# pygame 1.9.3
def main():
    pygame.draw.rect(screen,[235,235,235],(200,200,200,200))
    pygame.display.update()
    input('')
    # input is shown first instead of the rectangle

I expected the rectangle to draw first and then the input, but the input occured first and then the screen was updated

Arch
  • 33
  • 1
  • 4
  • Possible duplicate of [Why does graphical window freeze after about 5 seconds?](https://stackoverflow.com/questions/57718723/why-does-graphical-window-freeze-after-about-5-seconds) – Kingsley Nov 11 '19 at 00:10
  • The `input()` function is blocking the PyGame event/update/paint loop. Please see duplicate link. You could probably try calling `pygame.display.flush()` instead of the `.update()` - but it's blocking on input, so I'm not sure it would help. – Kingsley Nov 11 '19 at 00:11
  • i don't have that problem???? I tried your code, but it work normally as rectangle draw first, and then input. – okie Nov 11 '19 at 01:59
  • Do [`pygame.event.pump()`](https://www.pygame.org/docs/ref/event.html#pygame.event.pump) after `pygame.display.update()` and before `input('')` – Rabbid76 Nov 11 '19 at 05:46
  • @WhereisourMonica strange... I just tried it again and the same thing happened. Are you using the correct versions? – Arch Nov 12 '19 at 02:19
  • @Rabbid76 Wow! This actually worked! Would you mind explaining what pump() does? – Arch Nov 12 '19 at 02:59

1 Answers1

1

Call pygame.event.pump() after pygame.display.update() and before input(''):

def main():
    pygame.draw.rect(screen,[235,235,235],(200,200,200,200))
    pygame.display.update()

    pygame.event.pump()

    input('')

At some OS, pygame.display.update() respectively pygame.display.flip() doesn't update the display directly. It just invalidates the display and notifies the system to update the display. Actually the display is updated when the events are handled.
The events are either handled by either pygame.event.pump() or pygame.event.get() (Which is used in event loops, but would do the job as well). Note this instruction do not only handle the IO or user events, they handle a bunch of internal events too, which are required to run run the system.

Not all implementations on all OS behave the same. At some OS it is sufficient to call pygame.display.update(), that is the reason that not everyone at every system can reproduce the issue. But in this case it is never wrong to call pygame.event.pump().

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I understand this now! Thanks for replying – Arch Nov 13 '19 at 00:59
  • @Arch if you like that solution, you can accept it(clicking that check mark on the answer) to show that this is the best solution for the answer. – okie Nov 13 '19 at 02:10