How can I integrate Pygame and PyQt4?

1

I'm using Python 2.7 and Ubuntu 14.04.

I find myself trying to this to insert my pygame window into my PyQt4 window.

  

On some platforms it is possible to embed the pygame display in an existing window. To do this, the environment variable SDL_WINDOWID must contain a string with the id of the destination window. This variable is checked when the pygame display is initialized.

This is my code:

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 this does not work, just show my PyQt window. I do not know if what I'm doing is wrong, or if directly pygame can not be integrated with PyQt.

What should I do to be able to embed my pygame in frmMain ?

Thank you very much.

    
asked by Pablo Ladreyt 09.07.2016 в 20:07
source

1 answer

0

I found something similar and solved it with:

import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)

Before the pygame.init , play giving values to x e y to adjust to your window.

It's not elegant but it works:)

    
answered by 27.11.2016 в 07:02