How To Draw Texts in PyQt5

In this PyQt5 tutorial i want to show you How To Draw Texts in PyQt5. so we are going to use QPainter class for drawing Texts. the QPainter class performs low-level painting on widgets and other paint devices. QPainter provides highly optimized functions to do most of the drawing GUI programs require. It can draw everything from simple lines to complex shapes like pies and chords. It can also draw aligned text and pixmaps.

 

 

 

Also you can check more Python GUI articles in the below links

1: Kivy GUI Development 

2: Python TKinter GUI Development 

3: Psyide2 GUI Development 

4: wxPython GUI Development 

5: PyQt5 GUI Development Course

 

 

 

 

Complete source code for  How To Draw Texts in PyQt5

 

 

 

 

This is our main window class that extends from QMainWindow and we initialize some requirements for the window in this class, also we have called our InitWindow() method

in here.

 

 

 

 

This is the method that we set our window requirements like icon, title, width and height

that we have before created in the constructor of our main class.

 

 

 

 

So now this is our PaintEvent() method, this is method is related to our QPainter class and when we want to do drawing in PyQt5 we need to use this method.and we create our Texts using painter.drawText() that takes two arguments the first one is the x and y position and the second one is the text it self that we want to draw.

 

 

 

 

The QTextDocument class holds formatted text.QTextDocument is a container for structured rich text documents, providing support for styled text and various types of document elements, such as lists, tables, frames, and images. They can be created for use in a QTextEdit, or used independently.

 

 

 

 

Run the complete code and this will be the result.

How To Draw Texts in PyQt5
How To Draw Texts in PyQt5

 

 

 

 

 

Also you can watch the complete video for this article.

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×