File size: 791 Bytes
078ce08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QPaintEvent
from PyQt5.QtWidgets import QWidget
class BackgroundWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.background_image = None
def set_background_image(self, image_path):
self.background_image = QPixmap(image_path)
self.update()
def paintEvent(self, event: QPaintEvent):
painter = QPainter(self)
if self.background_image:
pixmap = self.background_image.scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
x_offset = (pixmap.width() - self.width()) // 2
y_offset = (pixmap.height() - self.height()) // 2
painter.drawPixmap(-x_offset, -y_offset, pixmap)
|