Python Forum
[PyQt] PyQt5 graphics question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] PyQt5 graphics question
#1
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()
Reply
#2
There is an example

https://www.geeksforgeeks.org/create-ana...in-python/
Reply
#3
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.
Reply
#4
You can set a background image with stylesheet

self.setStyleSheet('background-image: url("/folder/image.png"); background-repeat: no-repeat; background-position: center;')
Reply
#5
(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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,838 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [Tkinter] Need some basic help with GUI graphics Jerz 2 2,969 Nov-11-2017, 12:10 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020