Dear All,
I wrote a simple wxPython GUI program. It has a frame, and on the frame I put a panel. And on the panel 3 buttons, start, stop, exit. I also plot on the panel a static bitmap. What I want is to plot continuously in a certain frequency images till the user press the stop button. Therefore I use a thread with a callback function. On windows my program runs very well with no problem at all. But when I start it on windows and press the start button I get the following strange error
Xlib: unexpected async reply (sequence 0x76b)!
I’ve searched on the internet and saw that this problem may be cause when you write things to your frame from the thread. But I thought that I was not doing this as I use a wx.PostEvent to refresh my image on the frame. Could someone please help me out with this problem. Her below I post the python script I have.
I
wanted to know also how I disable the (x) fucntionality on my main frame so that the user is obliged to close the program by pressing the exit button. Or is there a way to activate the exit function bind to the exit button when the user pressed the (x) sign on the main frame window ?
Many thanks in advance.
import wx
import cStringIO
import array
import time
import threading
import scipy
import cStringIO
[
EVT_UPDATE_ID, FRAME_ID
] = [wx.NewId() for i in range(1)]
class UpdateImageEvent( wx.PyEvent ):
“”"
Event Class to update the captured image
“”"
def init(self, bmp ):
“”"
@param tekst : bmp image
“”"
wx.PyEvent.init(self)
self.SetEventType( EVT_UPDATE_ID )
self.bmp = bmp
class imageViewerThread( threading.Thread ):
def init( self, refFrame ):
threading.Thread.init( self )
self._finished = threading.Event()
self._interval = 0.05
self._refFrame = refFrame
self.stopTask = None
def init( self ):
pass
def exit( self ):
pass
def setInterval( self, interval
):
self._interval = interval
def shutdown( self ):
self.stopTask = True
def run( self ):
self.stopTask = False
while not self.stopTask:
self.task()
self._finished.wait( self._interval )
self.exit()
def task( self ):
data = open( "temp.tif", "rb").read()
stream = cStringIO.StringIO(data)
bmp = wx.BitmapFromImage(
wx.ImageFromStream( stream ) )
wx.PostEvent( self._refFrame, UpdateImageEvent( bmp ) )
class MyFrame(wx.Frame):
def init(
self, parent, ID, title, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
):
wx.Frame.init(self, parent, ID, title, pos, size, style)
self._parent = parent
Font1 = wx.Font( 10, wx.DEFAULT, wx.NORMAL,
wx.BOLD, False,
“”, wx.FONTENCODING_SYSTEM )
self.SetMinSize( size )
self.SetMaxSize( size )
self.SetSize( size )
self.panel = wx.Panel( self )
self.btn = wx.Button( self.panel, -1, “Start”, pos=( 10,10 ) )
self.btnStop = wx.Button( self.panel, -1, “Stop”, pos=( 10,40 ) )
self.btnExit = wx.Button( self.panel, -1,
“Exit”, pos=( 10,150 ) )
# bind the button events to handlers
self.Bind(wx.EVT_BUTTON, self.OnButtonPlot, self.btn )
self.Bind(wx.EVT_BUTTON, self.OnButtonStop, self.btnStop )
self.Bind(wx.EVT_BUTTON, self.OnButtonExit, self.btnExit )
self.staticBitmap =
wx.StaticBitmap( self.panel, -1, pos=(150,20), size=(350, 450) )
# set default image
data = open( “image.jpg”, “rb”).read()
stream = cStringIO.StringIO(data)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream
) )
self.staticBitmap.SetBitmap( bmp )
self.staticBitmap.Refresh()
self.mThread = None
self.Connect( wx.ID_ANY, wx.ID_ANY,
FRAME_ID,
self.UpdateImage )
def UpdateImage( self, event ):
self.staticBitmap.SetBitmap( event.bmp )
self.staticBitmap.Refresh()
def OnButtonPlot( self, evt
):
self.mThread = imageViewerThread( self.panel, exposureTime )
self.mThread.init()
self.mThread.start()
self.btn.Enable( False )
self.btnStop.Enable( True )
self.btnExit.Enable( False )
def OnButtonStop( self, evt ):
self.mThread.shutdown()
del self.mThread
self.mThread = None
self.btn.Enable( True )
self.btnStop.Enable( False )
self.btnExit.Enable( True )
def
OnButtonExit(self, event):
self.Close(True)
self.Destroy()
class main(wx.App):
def OnInit(self):
win = MyFrame(None, FRAME_ID, “GigE Viewer”, size=( 850, 550 ),
style = wx.DEFAULT_FRAME_STYLE)
win.Show( True )
return True
application = main(0)
application.MainLoop()
···
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.