Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

python - How to integrate Pygame and PyQt4?

I'm using python 2.7 and Ubuntu 14.04.

I'm trying to do this in order to have my pygame window inside my GUI

On some platforms it is possible to embed the pygame display into an already existing window. To do this, the environment variable SDL_WINDOWID must be set to a string containing the window id or handle. The environment variable is checked when the pygame display is initialized

So this is what I did:

from PyQt4 import QtGui, QtCore
import os
import subprocess
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.setWindowModality(QtCore.Qt.ApplicationModal)
    MainWindow.setFixedSize(800, 600)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
    self.iniMap()

def iniMap(self):
    command = "xprop -root _NET_ACTIVE_WINDOW"
    output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
    activeWindowID = str(output.communicate()[0].decode("utf-8").strip().split()[-1])
    os.environ['SDL_WINDOWID'] = activeWindowID
    import pygame
    pygame.init()
    screen = pygame.display.set_mode((565, 437), pygame.NOFRAME)

class frmMain(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(frmMain, self).__init__(parent, flags=QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setupUi(self)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    form = frmMain()
    form.show()
    sys.exit(app.exec_())

But it doesn't work. It only shows my PyQt window. I don't know whether I'm doing something wrong or pygame just cannot be integrated with PyQt

What should I do to get my pygame window embedded in frmMain?

Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a sample solution as per the comment above:

from PyQt4 import QtGui
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)

    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_()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...