本文介绍了Pygame嵌入PyQt时不返回事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个应用程序,并且UI使用嵌入了Pygame的PyQt4.它使用计时器来更新"自身,这样可以说出来,并且在timerEvent函数中,Pygame尝试检索所有检测到的事件.问题是,Pygame没有检测到任何事件.

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.

这是我代码的简约版本

#!/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_()

当Pygame窗口嵌入PyQt应用程序中时,如何获取Pygame事件?

How can I get Pygame events while the Pygame window is embedded in a PyQt application?

推荐答案

您不能.

您不能也不应将两个具有自己的事件循环的库组合在一起,例如,现在Qt eventloop正在阻止pygame事件循环.

You cannot and should not combine 2 libraries that have their own event loop, for example now the Qt eventloop is blocking the pygame event loop.

这篇关于Pygame嵌入PyQt时不返回事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:40