ylliX - Online Advertising Network
Vertex AI - Antrophic and Mistral models: Why does it require Imegen access?

PyQT5: Center Align a label in a window


I’m very new to PyQT5 and I am just attempting to get a center aligned image in a window. Setting the alignment with label.setAlignment(QtCore.Qt.AlignCenter) does not seem to change anything from the default left alignment. What am I doing wrong?

class ImageLabel(QLabel):

    def __init__(self, parent=None):
        super().__init__(parent)

    def setPixmap(self, pixmap):
        super().setPixmap(pixmap)
        self.resize(self.pixmap().size())

class Window(QMainWindow):

    def __init__(self,imageName, max_width, max_height):
        super().__init__()
        self.initUI(imageName, max_width, max_height)

    def initUI(self, imageName, max_width, max_height):
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
        self.setGeometry(100, 100, 800, 600)

        self.imageLabel = ImageLabel(self)
        pixmap = QPixmap(imageName)
        scaled_pixmap = pixmap.scaled(max_width, max_height, QtCore.Qt.KeepAspectRatio)
        self.imageLabel.setPixmap(scaled_pixmap)
        self.imageLabel.setAlignment(QtCore.Qt.AlignCenter)

        self.showMaximized()

def image():
    imageName="test.png"
    App = QApplication(sys.argv)

    # Get the screen geometry
    screen_geometry = QDesktopWidget().availableGeometry()

    # Extract the maximum width
    max_width = screen_geometry.width()
    max_height = screen_geometry.height()

    # create the instance of our Window
    window = Window(imageName, max_width, max_height)
    # start the app
    App.exec_()

if __name__ == "__main__":
    image()



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *