如何集成Pygame和PyQt4?

2024-04-26 12:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python2.7和Ubuntu14.04。

我正在尝试this以便将pygame窗口放在我的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

我就是这么做的:

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

但没用。它只显示我的PyQt窗口。我不知道我是做错了什么还是pygame不能与PyQt集成

我该怎么做才能让pygame窗口嵌入到frmMain

提前谢谢你。


Tags: theimportselfinitisdefdisplaysys