如何整合Pygame和PyQt4?

时间:2016-07-09 08:51:21

标签: python python-2.7 pyqt pygame ubuntu-14.04

我正在使用python 2.7和Ubuntu 14.04。

我正在尝试this以便在我的GUI中使用我的pygame窗口

  

在某些平台上,可以将pygame显示嵌入到现有窗口中。为此,必须将环境变量SDL_WINDOWID设置为包含窗口ID或句柄的字符串。初始化pygame显示时检查环境变量

所以这就是我所做的:

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

提前谢谢。

1 个答案:

答案 0 :(得分:2)

以下是根据上述评论的示例解决方案:

{Type} {Field};
public {Type} {Property} {
  get {
    if ({Field} == null) {
      {Field} = new {Type}();
    }
    return {Field};
  }
}