Python Forum
Pyevent counterintuitive behaviour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyevent counterintuitive behaviour
#1
So, I'll make this very quick ( I picked up on pygame this week to try it out and also prototype some physics quickly ).

This does not close the window upon pressing q

while runner:
    screen.fill(color="black")

    keys = pg.key.get_pressed()

    if keys[pg.K_q]:
        runner = False
However, this stops the window upon pressing q

while runner:
    screen.fill(color="black")

    keys = pg.key.get_pressed()

    if keys[pg.K_q]:
        runner = False

    for event in pg.event.get():
        continue
Obviously, the event for loop is checking all events that pygame is getting from the OS, and only then knows to quit it?

In fact, the for loop is not even needed

while runner:
    screen.fill(color="black")

    keys = pg.key.get_pressed()

    if keys[pg.K_q]:
        runner = False
    print("Print and exit",pg.event.get())
Output:
Output:
.... Print and exit [] Print and exit [] Print and exit [] Print and exit [] Print and exit [<Event(768-KeyDown {'unicode': 'q', 'key': 113, 'mod': 0, 'scancode': 20, 'window': None})>, <Event(771-TextInput {'text': 'q', 'window': None})>] Print and exit []
I'm looking for someone to explain why does it not stop as soon as the conditional for the while loop is switched to false, and it actually needs to touch that get function. It feels redundant.
Reply
#2
You need to call pygame.event.pump() to update the key, or any event, info. Pygame.event.get() does this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 1,523 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 1,199 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 2,769 Apr-09-2023, 01:48 AM
Last Post: vugz
  Weird behaviour using if statement in python 3.10.8 mikepy 23 7,532 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Generator behaviour bla123bla 2 1,831 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 4,068 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 3,914 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 3,992 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,915 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa
  OOP behaviour problem JohnB 3 3,196 Aug-18-2020, 07:51 PM
Last Post: JohnB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020