Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't just magically put an <code>Event</code> from <em>outside</em> your application into pygames event queue.</p> <p>If you want to do something like this, you have to use some kind of IPC or networking.</p> <p>An easy way would be using UDP for example.</p> <p>This class will respond to UDP messages and put the event into <code>pygame</code>'s event queue:</p> <pre><code>import socket import pygame pygame.init() class UdpToPygame(): def __init__(self): UDP_IP="127.0.0.1" UDP_PORT=15006 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setblocking(0) self.sock.bind((UDP_IP,UDP_PORT)) def update(self): try: data, addr = self.sock.recvfrom(1024) ev = pygame.event.Event(pygame.USEREVENT, {'data': data, 'addr': addr}) pygame.event.post(ev) except socket.error: pass </code></pre> <p>In your <code>pygame</code> application, you would create an instance of that class and call the <code>update</code> method in your main loop.</p> <p>A sample application that responds to the custom events <code>UdpToPygame</code> will put into the queue:</p> <pre><code>def main(): dispatcher = UdpToPygame() screen = pygame.display.set_mode((800, 600)) l = 0 r = True while r: for ev in pygame.event.get(): if ev.type == pygame.QUIT: r = False if ev.type == pygame.USEREVENT: t = pygame.font.SysFont('', 40).render(ev.data, True, (255,255,255)) screen.blit(t, (0, l*20)) l += 1 dispatcher.update() pygame.display.flip() if __name__ == "__main__": main() </code></pre> <p>Run this and fire up a python interpreter to send text directly to your running pygame application:</p> <pre><code>&gt;&gt;&gt; import socket &gt;&gt;&gt; sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) &gt;&gt;&gt; while True: ... sock.sendto(raw_input(), ("127.0.0.1", 15006)) ... hallo 5 pygame.... 10 </code></pre> <p><strong>EDIT:</strong> </p> <p>As for your error <code>pygame.error: video system not initialized</code>:</p> <p>Ensure you initialized pygame with <code>pygame.init()</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload