He tenido algunos problemas con un programa que he estado escribiendo y agradecería alguna ayuda o aporte. Para algunos antecedentes, estoy usando Python 2.7 y wxPython para hacer un cliente de webcam en tiempo real. El cliente obtiene las imágenes del servidor en su propio hilo y las coloca en una cola. El hilo de la GUI obtiene esas imágenes de la cola y las convierte en un objeto wxBitmap
. Esto sucede cada 0,5 segundos y funciona genial. Puedo guardar el objeto wxBitmap
como un archivo, así sé que todo funciona correctamente.problemas al mostrar wxBitmaps usando wxPython
El problema que tengo es que el objeto wxBitmap
se muestre en mi GUI. Lo único que creo que puedo hacer con la GUI es mostrar un rectángulo gris donde debería estar la imagen de la cámara web.
Aquí es mi onPaint()
que se llama cuando quiero actualizar la pantalla:
def onPaint(self,e):
## this is the function that actually draws and redraws the window
## to be displayed. I think it is something similar to blit()
## in other graphical display frameworks
print "in onPaint"
## create the device context object (graphics painter)
dc = wx.PaintDC(self)
dc.BeginDrawing()
## draw the bitmap to the screen
dc.DrawBitmap(self.imageBit,0,0,True)
dc.EndDrawing()
## test code.
## the following works and updates, which means that
## everything is being converted properly and updated.
## not sure why the dc won't paint it to the window.
self.imageBit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP)
En pocas palabras, estoy en una pérdida de por qué no su trabajo. de mi investigación descubrí que debido a que estoy en una máquina con Windows necesitaba las funciones BeginDrawing()
y EndDrawing()
, así que las agregué. Aún no funciona. No hay errores o excepciones lanzadas.
otras preguntas que pueden ayudar a resolver este problema:
- estoy actualizando un objeto
wxFrame
. ¿Tal vez elwxPaintDC
necesita operar en otro tipo de contenedor para funcionar? - ?
En realidad, tal vez mi función __init__
es la que está teniendo el problema. ¿Estoy configurando esto correctamente?
class viewWindow(wx.Frame):
imgSizer = (480,360)
def __init__(self, *args, **kw):
## this is called when an instance of this class is created
super(viewWindow,self).__init__(*args,**kw)
## here is where the actual stuff inside the frame is set up.
self.pnl = wx.Panel(self)
## create a button that opens up a Connection Window
#test = wx.Button(self.pnl, label='Connection Settings')
## test.Bind(wx.EVT_BUTTON, self.openConnectionWindow)
## create the wxImage for the web cam pic
self.image = wx.EmptyImage(self.imgSizer[0],self.imgSizer[1])
## create the wxBitmap so that the wxImage can be displayed
self.imageBit = wx.BitmapFromImage(self.image)
## create a timer that will update the window based of frame rate
self.timex = wx.Timer(self, wx.ID_OK)
self.timex.Start(500)
self.Bind(wx.EVT_TIMER, self.redraw, self.timex)
## need to do the following in order to display images in wxPython:
self.Bind(wx.EVT_PAINT, self.onPaint)
self.SetSize(self.imgSizer)
self.SetTitle('View Window')
self.Show()
De todos modos, gracias de antemano por su ayuda.
EDIT: resolví el problema accidentalmente borrando la línea self.pnl = wx.Panel(self)
.
Aparentemente se estaba procesando correctamente, pero el mapa de bits estaba debajo del panel. ¿Tal vez? No estoy realmente seguro. Soy nuevo en esto de wxPython.
La imagen que aparece debajo del panel es una posibilidad. A menos que establezca explícitamente la posición de uso de un 'wx.Sizer' los objetos predeterminados a (0,0). Es por eso que uso un medidor incluso si solo tengo 1 artículo. – acattle