pyglet--EventLoop object (main event loop, used to take messages from the system message queue and dispatch to each window)

1. Identify the system message and send the message

EventLoop (the event loop of the application), which is used to obtain system messages (including various parameters of the message: such as mouse position, event type, left and right mouse buttons, which keyboard keys, etc.) from the system message queue, and then dispatch the corresponding Events are handled by the window.

2. Inherited from EventDispatcher

Due to the inheritance of the word EventDispatcher, you can add custom events and use an object as its event processing object (also called Listener) to process the events it dispatches. If the dispatched event happens to be a mouse or keyboard event, this can be very useful for the Listener to get the mouse position information or keyboard information. (For example, if you build a button object, you can know whether the mouse is clicked on yourself).

3. Uniqueness

A pyglet program can only have one EventLoop in a working state (that is, calling the run() function)

Fourth, the work done by each event loop

One is to obtain system events related to pyglet from the system event queue and dispatch them;

The second is to call the idle() function. The functions of this function include:

1. Call the clock.tick() function to run those scheduled functions (sheduled functions)

2. Dispatch the on_draw() event to redraw each open window.

(Personal understanding is that every time the event loops, the main task is to process system events, and then call the idle() function, because every time a system event is processed, idle is called, so as long as there is a user event, call idle, and all windows will be redrawn ,

If there are neither system events nor scheduled functions to execute, the system may not loop iterations)

5. The true face of event loop itaration

while True :
     pyglet . clock . tick ()#Start the clock object for the execution of the scheduled function for window in pyglet . app . windows :# Traverse each open window window . switch_to () window . dispatch_events ()#From the system Get events from the event queue and dispatch window . dispatch_event ( 'on_draw' )# redraw each window window . flip () # implement double buffering mechanism 
so the window is redrawn all the time

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324841103&siteId=291194637