Pygame, mouse events and threads

Discussion in 'Python' started by jedi200581@yahoo.co.uk, Aug 24, 2006.

  1. Guest

    Hi,

    I'm trying to retreive mouse event in a python thread using pygame.
    It's not working and I don't know why! Here is the code:

    <snippet on>

    import pygame, sys, os, threading
    from pygame.locals import *
    from threading import *

    class Display(Thread) :
    """Class managing display"""
    def __init__(self, xRes = 640, yRes = 480):
    Thread.__init__(self)
    #initializing display
    self._xRes = xRes
    self._yRes = yRes
    pygame.display.init()
    self._window = pygame.display.set_mode((xRes, yRes))
    pygame.display.set_caption('Display')
    self._screen = pygame.display.get_surface()
    pygame.display.flip()

    self._log = []
    def input(self, event):
    if event.type == KEYDOWN:
    sys.exit(0)
    else:
    print event
    def run(self):
    print "starting event handling thread"
    i = 0
    while True:
    self.input(pygame.event.wait())

    disp = Display()
    disp.start()

    <snippet off>

    When I put the content of the run and input functions in the main
    thread, it's working, why not in the thread?
     
    , Aug 24, 2006
    #1
    1. Advertising

  2. Ben Sizer Guest

    wrote:

    > When I put the content of the run and input functions in the main
    > thread, it's working, why not in the thread?


    Because event handling needs to be done in the main thread. So does
    rendering. This is a limitation of the underlying system.

    As a general rule, try to keep all your PyGame functions in the main
    thread and push your other processing into background threads, if you
    really need them.

    --
    Ben Sizer
     
    Ben Sizer, Aug 24, 2006
    #2
    1. Advertising

  3. Guest

    Ben Sizer wrote:
    > wrote:
    >
    > > When I put the content of the run and input functions in the main
    > > thread, it's working, why not in the thread?

    >
    > Because event handling needs to be done in the main thread. So does
    > rendering. This is a limitation of the underlying system.
    >
    > As a general rule, try to keep all your PyGame functions in the main
    > thread and push your other processing into background threads, if you
    > really need them.
    >
    > --
    > Ben Sizer


    Well, that is a limitation... And is it something that will be fixed or
    something that is inherent to pygame and not fixable?
     
    , Aug 24, 2006
    #3
  4. schrieb:
    > Ben Sizer wrote:
    >> wrote:
    >>
    >>> When I put the content of the run and input functions in the main
    >>> thread, it's working, why not in the thread?

    >> Because event handling needs to be done in the main thread. So does
    >> rendering. This is a limitation of the underlying system.
    >>
    >> As a general rule, try to keep all your PyGame functions in the main
    >> thread and push your other processing into background threads, if you
    >> really need them.

    > Well, that is a limitation... And is it something that will be fixed or
    > something that is inherent to pygame and not fixable?


    I guess it is part of the SDL pygame sits upon. And I fail to see where
    that is a limitation. There can't be multiple event loops - only one.
    And if it has to be the main thread, do whatever you wanted to do there,
    and whatever your main thread was supposed to do is run in a separate
    thread.


    Diez
     
    Diez B. Roggisch, Aug 24, 2006
    #4
  5. Ben Sizer Guest

    wrote:
    > Ben Sizer wrote:
    > > wrote:
    > >
    > > > When I put the content of the run and input functions in the main
    > > > thread, it's working, why not in the thread?

    > >
    > > Because event handling needs to be done in the main thread. So does
    > > rendering. This is a limitation of the underlying system.
    > >
    > > As a general rule, try to keep all your PyGame functions in the main
    > > thread and push your other processing into background threads, if you
    > > really need them.

    >
    > Well, that is a limitation... And is it something that will be fixed or
    > something that is inherent to pygame and not fixable?


    It's inherent to SDL (the library underpinning PyGame), because it's
    apparently inherent to some of the platforms it runs on. Rendering and
    event handling is typically tied closely to the notion of a window and
    that in turn may well be coupled tightly to a single process or thread.
    As Diez said however, this is not generally a big problem. Most games
    are typically single-threaded anyway.

    --
    Ben Sizer
     
    Ben Sizer, Aug 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    614
    Gernot Frisch
    Dec 15, 2005
  2. rantingrick

    [pygame-bug] Pygame.cdrom bug

    rantingrick, Jan 30, 2011, in forum: Python
    Replies:
    1
    Views:
    338
    Benjamin Kaplan
    Jan 30, 2011
  3. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    423
    Chris
    Aug 30, 2005
  4. Vojtěch Polášek

    delay on windows when processing Pygame events

    Vojtěch Polášek, Sep 10, 2012, in forum: Python
    Replies:
    0
    Views:
    210
    Vojtěch Polášek
    Sep 10, 2012
  5. Alex Gardner

    Pygame mouse cursor load/unload

    Alex Gardner, Mar 3, 2013, in forum: Python
    Replies:
    26
    Views:
    372
    Alex Gardner
    Mar 18, 2013
Loading...

Share This Page