Python Forum

Full Version: PyQt5 graphics question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!
Below my first Python PyQt5 app. Cool
So far it works perfectly.
But what I do in paintEvent is to load the background image every time the timer fires and I think that could be done better. But how ???
Thanks
Hein

import sys
import numpy
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets, uic


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        # timer
        timer = QTimer(self)
        timer.timeout.connect(self.update)
        timer.start(100)

        # main window
        self.setWindowTitle("Uhren II")
        self.setFixedSize(500, 500)
        self.setWindowIcon(QIcon("Uhren2.ico"))

        self.label = QtWidgets.QLabel()
        canvas = QtGui.QPixmap(500, 500)
        canvas.fill(Qt.white)
        pixmap = QPixmap("./pic/Uhr001.jpg")
        self.label.setPixmap(pixmap)
        self.resize(pixmap.width(),pixmap.height())
        self.setCentralWidget(self.label)
        
    def paintEvent(self, event):
        dt = QTime.currentTime()
        dts = dt.toString()
        self.setWindowTitle("Uhren II       " + dts)
        pixmap = QPixmap("./pic/Uhr001.jpg")
        self.label.setPixmap(pixmap)
        painter = QtGui.QPainter(self.label.pixmap())
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        x0 = 244
        y0 = 248
        rs = 110
        rm = 100
        rh = 70
        col = QColor(0, 0, 0)
        datum = 1
        datumF = 3
        datumPx = 245
        datumPy = 319
        s = dt.second() + dt.msec() / 1000
        m = dt.minute() + s / 60
        h = dt.hour() + m / 60
        if (h > 12):
            h -= 12
        # h
        x1 = x0 + rh * 0.2 * numpy.sin(numpy.pi / 6 * h + numpy.pi)
        y1 = y0 - rh * 0.2 * numpy.cos(numpy.pi / 6 * h + numpy.pi)
        x2 = x0 + rh * numpy.sin(numpy.pi / 6 * h)
        y2 = y0 - rh * numpy.cos(numpy.pi / 6 * h)
        painter.setPen(QPen(col, 10, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin));
        painter.drawLine(int(x1), int(y1), int(x2), int(y2))
        # m
        x1 = x0 + rm * 0.2 * numpy.sin(numpy.pi / 30 * m + numpy.pi)
        y1 = y0 - rm * 0.2 * numpy.cos(numpy.pi / 30 * m + numpy.pi)
        x2 = x0 + rm * numpy.sin(numpy.pi / 30 * m)
        y2 = y0 - rm * numpy.cos(numpy.pi / 30 * m)
        painter.setPen(QPen(col, 5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin));
        painter.drawLine(int(x1), int(y1), int(x2), int(y2))
        # s
        x1 = x0
        y1 = y0
        x2 = x0 + rs * numpy.sin(numpy.pi / 30 * s)
        y2 = y0 - rs * numpy.cos(numpy.pi / 30 * s)
        painter.setPen(QPen(QColor(255, 0, 0), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin));
        painter.drawLine(int(x1), int(y1), int(x2), int(y2))

app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Thanks Axel Erfurt, I found this article yesterday but it didn't really help because he didn't use a background image for the clock.
You can set a background image with stylesheet

self.setStyleSheet('background-image: url("/folder/image.png"); background-repeat: no-repeat; background-position: center;')
(Jan-17-2023, 07:02 PM)Axel_Erfurt Wrote: [ -> ]You can set a background image with stylesheet

self.setStyleSheet('background-image: url("/folder/image.png"); background-repeat: no-repeat; background-position: center;')

Ah. Thanks, I'll try that tomorrow.
Keep the background pixmap as an attribute of the class. In the paint event make a copy() of the pixmap instead of loading from a file. Draw the clock hands on the copy. Set the label pixmap to the copy.