In this PyQt5 article we want to learn How to Create 3D Graphics in PyQt5, PyQt5 is powerful library for creating graphical user interfaces (GUIs) in Python. It provides different tools and widgets for building desktop applications, but it can also be used for creating 3D graphics. in this article we want to explore how to create 3D graphics with PyQt5.
How to Create 3D Graphics in PyQt5
First of all we need to install PyQt5 library
1 |
pip install PyQt5 |
Also we need to Pyopengl library. PyOpenGL is Python wrapper for the OpenGL graphics API. it allows Python developers to create high performance 3D graphics applications using OpenGL functions. PyOpenGL can be installed using pip:
1 |
pip install PyOpenGL |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import sys import os from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtOpenGL import * from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * os.environ['QT_OPENGL'] = 'desktop' class MyOpenGLWidget(QGLWidget): def initializeGL(self): glClearColor(0.0, 0.0, 0.0, 1.0) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, self.width()/self.height(), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) def paintGL(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glPushMatrix() glRotatef(30, 1.0, 0.0, 0.0) glRotatef(30, 0.0, 1.0, 0.0) vertices = ( ( 1, -1, -1), ( 1, 1, -1), (-1, 1, -1), (-1, -1, -1), ( 1, -1, 1), ( 1, 1, 1), (-1, -1, 1), (-1, 1, 1) ) 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) ) glBegin(GL_LINES) for edge in edges: for vertex in edge: glVertex3fv(vertices[vertex]) glEnd() glPopMatrix() error = glGetError() if error != GL_NO_ERROR: print('OpenGL Error:', error) if __name__ == '__main__': app = QApplication(sys.argv) # create a QGLFormat with the required OpenGL version format = QGLFormat() format.setVersion(3, 3) format.setProfile(QGLFormat.CoreProfile) format.setDepthBufferSize(24) QGLFormat.setDefaultFormat(format) widget = MyOpenGLWidget() widget.show() sys.exit(app.exec_()) |
In this code we have added the necessary OpenGL code to create a cube using glBegin and glEnd. glPushMatrix and glPopMatrix calls are used to preserve the current matrix state so that only the cube is rotated and not the entire scene. glRotatef calls are used to rotate the cube around the x and y axes. gluPerspective and gluLookAt calls are used to set up the camera projection and view matrices. Finally, I added glEnable(GL_DEPTH_TEST), glEnable(GL_LIGHTING), and glEnable(GL_LIGHT0) to enable depth testing, lighting, and the first light source.
Run the complete code and this will be the result

Learn More on PyQt5
- How to Deploy PyQt5 Applications
- How to Integrate PyQt5 with OpenCV
- How to Use Stylesheets in PyQt5
- Learn PyQt5 Event Handling with Signals and Slots
- PyQt5 Tutorial: Creating Menus and Toolbars
- How to Open & Save Files in Python PyQt5
- PyQt5 Designer Tutorial: Complete Guide to Qt Designer
- PyQt5 vs Tkinter: Which GUI Library is Right for You
- How to Build Web Applications in PyQt5
- How to Integrate Python Flask with PyQt5