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):
ThePainter = QPainter()
ThePainter.begin(self)
ThePainter.setBrush(QColor(200, 0, 0))
ThePainter.drawRect(10, 40, 90, 60)
ThePainter.setBrush(QColor(0, 200, 0))
ThePainter.drawEllipse(10, 110, 90, 60)
ThePen=QPen(QColor(120,120,120))
ThePen.setWidth(5)
ThePainter.setPen(ThePen)
ThePainter.drawLine(10, 230, 90, 260)
ThePainter.setPen(ThePen)
ThePainter.setBrush(QColor(0, 200, 0))
ThePainter.setFont(QFont('Decorative', 10))
ThePainter.drawText(event.rect(), Qt.AlignLeft, "This is some text that I drew into the window")
ThePainter.end()
######################################################################################################
# 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()
TheFrame = QFrame(self)
TheFrame.setFrameShape(QFrame.StyledPanel)
MainLayout.addWidget(TheFrame)
SettingsLayout = QVBoxLayout()
TheFrame.setLayout(SettingsLayout)
#########################################################
# Create a canvas to draw into
self.TheCanvas = CanvasWidget()
SettingsLayout.addWidget(self.TheCanvas)
#########################################################
# 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)
TheWindow = WindowClass()
sys.exit(TheApplication.exec_())
© Copyright 2018 HSU - All rights reserved.