Issue
I'm testing out an application and the UI uses PyQt4 with Pygame embedded into it. It uses a timer to "update" itself so to speak and in the timerEvent function Pygame attempts to retrieve all detected events. Issue is, Pygame isn't detecting any events.
Here's a minimalist version of my code
#!/etc/python2.7
from PyQt4 import QtGui
from PyQt4 import QtCore
import pygame
import sys
class ImageWidget(QtGui.QWidget):
def __init__(self,surface,parent=None):
super(ImageWidget,self).__init__(parent)
w=surface.get_width()
h=surface.get_height()
self.data=surface.get_buffer().raw
self.image=QtGui.QImage(self.data,w,h,QtGui.QImage.Format_RGB32)
self.surface = surface
self.timer = QtCore.QBasicTimer()
self.timer.start(500, self)
def timerEvent(self, event):
w=self.surface.get_width()
h=self.surface.get_height()
self.data=self.surface.get_buffer().raw
self.image=QtGui.QImage(self.data,w,h,QtGui.QImage.Format_RGB32)
self.update()
for ev in pygame.event.get():
if ev.type == pygame.MOUSEBUTTONDOWN:
print "Mouse down"
def paintEvent(self,event):
qp=QtGui.QPainter()
qp.begin(self)
qp.drawImage(0,0,self.image)
qp.end()
class MainWindow(QtGui.QMainWindow):
def __init__(self,surface,parent=None):
super(MainWindow,self).__init__(parent)
self.setCentralWidget(ImageWidget(surface))
pygame.init()
s=pygame.Surface((640,480))
s.fill((64,128,192,224))
pygame.draw.circle(s,(255,255,255,255),(100,100),50)
app=QtGui.QApplication(sys.argv)
w=MainWindow(s)
w.show()
app.exec_()
How can I get Pygame events while the Pygame window is embedded in a PyQt application?
Solution
Fist of all do not mix frameworks. The frameworks may interact poorly or completely conflict with one another. Getting it to work on your system doesn't mean it will work on another system or with a different version of any of the frameworks. Mixing frameworks always means some kind of undefined behavior.
In your example your create an image (pygame.Surface) with the Pygame library and display it in QWidget
.
You never create a Pygame window. Therefore the Pygame event handling cannot work. You need to use Qts event handling.
Anyway, if you just want to do some image processing or draw some pictures and display them in a Qt application, I suggest using OpenCV (cv2). This library is designed for powerful image manipulation and the images can be viewed nicely using a Qt user interface.
Answered By - Rabbid76
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.