473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Pygame, mouse events and threads

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 '06 #1
4 3588
je********@yahoo.co.uk 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

Aug 24 '06 #2

Ben Sizer wrote:
je********@yahoo.co.uk 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 '06 #3
je********@yahoo.co.uk schrieb:
Ben Sizer wrote:
>je********@yahoo.co.uk 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
Aug 24 '06 #4
je********@yahoo.co.uk wrote:
Ben Sizer wrote:
je********@yahoo.co.uk 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

Aug 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Erik Bethke | last post by:
Hello All, I am trying to clean up some polish bugs with the Shanghai game I am working on and I am currently stuck on trying to get the right event for detecting when the user has changed the...
3
by: Tim Knauf | last post by:
Hi everyone, I'm glad to have found this list. I've written a small script for my own use which, amongst other things, captures mouse click information from a window containing an image. I used...
1
by: kjm | last post by:
Hi everyone, I have recently acquired a Logitech Rumble pad to use as an input device. I have been having trouble getting the event que to respond that a button or hat arrow has been pressed. ...
0
by: Lunpa | last post by:
My project: I'm working on a game, where in the ui, it takes the pygame window, and shoves it into a gtk2 socket widget. (gtk2 widgets are generated with glade, with the exception of the socket...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.