I am trying to make a threaded window to display PIL Images. As it is I have a window working that can display one image running on the following thread. I use an event based system to put images into frame.
The code works, but I'd like to be able to open up multiple frames, multiple images. If I try to move anything out of the Thread.run() method it crashes. Here is the code:
class window_thread(threading.Thread):
"""Run the MainLoop as a thread. Access the frame with self.frame."""
def __init__(self, name, autoStart=True):
threading.Thread.__init__(self)
self.setDaemon(1)
self.start_orig = self.start
self.start = self.start_local
self.frame = None #to be defined in self.run
self.name = 'Picture: ' + (name or 'Untitled')
self.lock = threading.Lock()
self.lock.acquire() #lock until variables are set
if autoStart:
self.start() #automatically start thread on init
def run(self):
app = wx.PySimpleApp()
frame = show_frame(None)
frame.SetSize((800, 600))
frame.SetTitle(self.name)
frame.Show(True)
#define frame and release lock
#The lock is used to make sure that SetData is defined.
self.frame = frame
self.lock.release()
app.MainLoop()
def start_local(self):
self.start_orig()
#After thread has started, wait until the lock is released
#before returning so that functions get defined.
self.lock.acquire()
I was thinking of making a Queue with jobs for the thread to do, like making new frames. To do this, though, I think I'd need to make a custom mainloop so that I can check for jobs in the Queue, and I have no idea where to begin.
Any help on a custom mainloop would be great, or alternate ideas as to creating multiple frames in this thread.
Thanks,
Chris Maddison
You can only do UI work from the main thread. It's not clear
to me if your code is running as a separate thread, but the
symptoms are consistent with that. Yes, it's a pain. You
could do all the image prep in a thread and have the main thread
perform the actual display. Use pubsub for communicating (it's
been discussed a lot here recently).
Good luck,
Phil
···
At 01:38 PM 8/15/2008, you wrote:
I am trying to make a threaded window to display PIL Images. As it is
I have a window working that can display one image running on the
following thread. I use an event based system to put images into frame.
The code works, but I'd like to be able to open up multiple frames,
multiple images. If I try to move anything out of the Thread.run()
method it crashes. Here is the code: [snip]
Thanks, that's a real shame. But, I'll test out those alternatives.
Chris Maddison
···
On 15-Aug-08, at 2:20 PM, Phil Mayes wrote:
At 01:38 PM 8/15/2008, you wrote:
I am trying to make a threaded window to display PIL Images. As it is
I have a window working that can display one image running on the
following thread. I use an event based system to put images into frame.
The code works, but I'd like to be able to open up multiple frames,
multiple images. If I try to move anything out of the Thread.run()
method it crashes. Here is the code: [snip]
You can only do UI work from the main thread. It's not clear
to me if your code is running as a separate thread, but the
symptoms are consistent with that. Yes, it's a pain. You
could do all the image prep in a thread and have the main thread
perform the actual display. Use pubsub for communicating (it's
been discussed a lot here recently).
Good luck,
Phil
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Pubsub does not handle interthread communication. For that, you
should stick with a Queue instance, wx.CallAfter, and/or wx.PostEvent.
- Josiah
···
On Fri, Aug 15, 2008 at 2:20 PM, Phil Mayes <olivebr@olivebr.com> wrote:
At 01:38 PM 8/15/2008, you wrote:
I am trying to make a threaded window to display PIL Images. As it is
I have a window working that can display one image running on the
following thread. I use an event based system to put images into frame.
The code works, but I'd like to be able to open up multiple frames,
multiple images. If I try to move anything out of the Thread.run()
method it crashes. Here is the code: [snip]
You can only do UI work from the main thread. It's not clear
to me if your code is running as a separate thread, but the
symptoms are consistent with that. Yes, it's a pain. You
could do all the image prep in a thread and have the main thread
perform the actual display. Use pubsub for communicating (it's
been discussed a lot here recently).
Good luck,
Phil
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Chris Maddison wrote:
Thanks, that's a real shame. But, I'll test out those alternatives.
Chris Maddison
I am trying to make a threaded window to display PIL Images. As it is
I have a window working that can display one image running on the
following thread. I use an event based system to put images into frame.
The code works, but I'd like to be able to open up multiple frames,
multiple images. If I try to move anything out of the Thread.run()
method it crashes. Here is the code: [snip]
You can only do UI work from the main thread. It's not clear
to me if your code is running as a separate thread, but the
symptoms are consistent with that. Yes, it's a pain. You
could do all the image prep in a thread and have the main thread
perform the actual display. Use pubsub for communicating (it's
been discussed a lot here recently).
Good luck,
Phil
As I understand it, this is pretty common in GUI frameworks. Most don't seem to provide a way to access GUI methods without blocking the main GUI thread. It makes sense...if you're accessing the GUI's methods, than you're blocking the event queue, so nothing happens in the GUI. Or you could interrupt
something else it's doing.
Anyway, Phil's got the right idea...but you'll need to use wx.CallAfter or wx.PostEvent instead of pubsub directly as those methods are thread-safe. You could make a pubsub call in wx.CallAfter though...
···
On 15-Aug-08, at 2:20 PM, Phil Mayes wrote:
At 01:38 PM 8/15/2008, you wrote:
-------------------
Mike Driscoll
Blog: http:\\blog.pythonlibrary.org
Python Extension Building Network: http:\\www.pythonlibrary.org