The sample below shows a simple example of painting in a window with the Qt library.
############################################################################
# Projection Explorer for Python
# This version is specifically for developing the code that provides
# Projection grids for websites
#
# http://zetcode.com/gui/pyqt5/painting/
# https://doc.qt.io/qt-5/qpen.html
############################################################################
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5 import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor, QFont,QPen
######################################################################################################
# Main Window
######################################################################################################
class CanvasWidget(QLabel):
########################################################################################################
# Construction Functions
########################################################################################################
def __init__(self):
super().__init__()
########################################################################################################
def paintEvent(self, event):
# Create a painting object
ThePainter = QPainter()
# Do the painting
ThePainter.begin(self) # everything between here and the end() call will be drawn into the Widget
ThePainter.setBrush(QColor(200, 0, 0)) # set the color of a brush. Brushes fill shapes
ThePainter.drawRect(10, 40, 90, 60) # use the brush to draw a rectangle
ThePainter.setBrush(QColor(0, 200, 0)) # set a different color
ThePainter.drawEllipse(10, 110, 90, 60) # draw an ellipse
ThePen=QPen(QColor(120,120,120)) # set a color for a pen. Pens draw lines and outlines
ThePen.setWidth(5)
ThePainter.setPen(ThePen) # set the pen into the painter
ThePainter.drawLine(10, 230, 90, 260) # draw a line
ThePainter.setPen(ThePen)
ThePainter.setFont(QFont('Decorative', 20)) # setup the font and size of the text
ThePainter.drawText(event.rect(), Qt.AlignLeft, "This is some text that I drew into the window")
ThePainter.end() # finishing drawing
######################################################################################################
# Main Window
######################################################################################################
class WindowClass(QWidget):
"""
A simple window class that includes some painting
"""
##################################################################################################
## Initialization
def __init__(self):
super().__init__()
#########################################################
# Create a vertical layout box for the entire window
MainLayout = QVBoxLayout()
# Add a frame to the main window's layout
TheFrame = QFrame(self)
TheFrame.setFrameShape(QFrame.StyledPanel)
MainLayout.addWidget(TheFrame)
# Add a box layout into the frame
SettingsLayout = QVBoxLayout()
TheFrame.setLayout(SettingsLayout)
#########################################################
# Create a canvas to draw into
self.TheCanvas = CanvasWidget() # create the canvas widget that will do the painting
SettingsLayout.addWidget(self.TheCanvas) # add the canvas to the layout within the frame
#########################################################
# Setup the overall window
# Set the vertical layout as the main layout for the window
self.setLayout(MainLayout)
# set the initial size of the window
self.setGeometry(100, 100, 600, 600)
# Set the title of the window and call show() to make the window appear
self.setWindowTitle('Painting')
self.show()
########################################################################
TheApplication = QApplication(sys.argv) # create a PyQt application
TheWindow = WindowClass() # Create our window object
sys.exit(TheApplication.exec_()) # Start the application
© Copyright 2018 HSU - All rights reserved.