Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pygame, mouse events and threads

Reply
Thread Tools

Pygame, mouse events and threads

 
 
jedi200581@yahoo.co.uk
Guest
Posts: n/a
 
      08-24-2006
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?

 
Reply With Quote
 
 
 
 
Ben Sizer
Guest
Posts: n/a
 
      08-24-2006
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

 
Reply With Quote
 
 
 
 
jedi200581@yahoo.co.uk
Guest
Posts: n/a
 
      08-24-2006

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?

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-24-2006
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
 
Reply With Quote
 
Ben Sizer
Guest
Posts: n/a
 
      08-25-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Heres a mouse theres a mouse what a mouse do? unholy Gaming 37 09-17-2006 08:59 AM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
How to catch Mouse down and mouse up on item of treeview xuanqn09@yahoo.com C++ 2 12-15-2005 01:54 PM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
problem with Java Web Start and Mouse and Key Events evianhat Java 0 04-19-2004 11:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57