[Python] How do I set up PyOpenGL with PyQt5?

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am wanting to set up several 3D mathematical projects with python. The best way I can see to render these is with PyOpenGL. I also want to run it in PyQt5, so that I can have GUI’s along side the render. All the information I can find is either using PyGame or QtDesigner. I would like to work without QtDesigner. Does anyone know where I could find a tutorial on how to set this up?

    EDIT:

    I managed to get some web scrounging done. I found the following code at https://pythonprogramming.net/community/37/Cube rotation with pyopengl and pyqt/ where the author asks for help regarding it not running. he says the following about it:


    I'm very new to python. I have a problem with my code, it's a very simple rotating cube. I don't have any problem with this cube code with pygame screen but when I use it with pyqt (or Qt designer widgets), it runs but it shows nothing!!!
    ​

    I copied his code into my IDe, then saved it as cubes.py. I opened up a CMD instance in thae directory of the file and called it. It opened a tiny black Qt window in the middle of the screen: [​IMG]

    When I try to resize the window by dragging the corner, it throws a deep traceback:

    File "C:\Users\aweso\Documents\Python\cubes\cubes.py", line 1, in <module>
    from OpenGL.GL import *
    ModuleNotFoundError: No module named 'OpenGL'

    C:\Users\aweso\Documents\Python\cubes>cubes.py
    Traceback (most recent call last):
    File "C:\Users\aweso\Documents\Python\cubes\cubes.py", line 56, in paintGL
    glEnd()
    File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\latebind.py", line 63, in __call__
    return self.wrapperFunction( self.baseFunction, *args, **named )
    File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\GL\exceptional.py", line 45, in glEnd
    return baseFunction( )
    File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\platform\baseplatform.py", line 415, in __call__
    return self( *args, **named )
    File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\error.py", line 234, in glCheckError
    baseOperation = baseOperation,
    OpenGL.error.GLError: GLError(
    err = 1280,
    description = b'invalid enumerant',
    baseOperation = glEnd,
    cArguments = ()
    )


    Here is his code, unmodified:

    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL import *
    from PyQt5.QtOpenGL import *
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import *
    import sys,time

    class MainWindow(QGLWidget):
    def __init__(self):
    super(MainWindow, self).__init__()

    self.widget = glWidget(self)
    mainLayout = QVBoxLayout()
    mainLayout.addWidget(self.widget)
    self.setLayout(mainLayout)

    class glWidget(QGLWidget):

    def __init__(self, parent):
    QGLWidget.__init__(self, parent)

    #self.setMinimumSize(400, 400)

    self.verticies = (
    (1,-1,-1),
    (1,1,-1),
    (-1,1,-1),
    (-1,-1,-1),
    (1,-1,1),
    (1,1,1),
    (-1,-1,1),
    (-1,1,1))
    self.edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7))

    def paintGL(self):
    while True:
    #glRotatef(1,3,1,1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glBegin(GL_LINE)
    for self.edge in self.edges:
    for self.vertex in self.edge:
    glVertex3fv(self.verticies[self.vertex])
    glEnd()
    glFlush()
    time.sleep(1)

    def resizeGL(self, w, h):
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-50, 50, -50, 50, -50.0, 50.0)
    glViewport(0, 0, w, h)

    def initializeGL(self):

    #glClearColor(0.0, 0.0, 0.0, 1.0)
    gluPerspective(45,800/600,0.1,50.0)
    glTranslatef(0.0,0.0,-5)
    glRotatef(0,0,0,0)

    if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

    Continue reading...

Compartilhe esta Página