1

I'm working on a UI in PyQt and I want to add a background-image to the MainWindow. Adding the Picture is not the problem, but if I run my code the image is shown multiple times...

Here is a short codesnipped:

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *

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

            self.setWindowTitle(f'Background Issue')
            self.setMinimumSize(1000, 800)
            self.setStyleSheet("background-image: url(max.svg);")

    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = BackgroundIssue()
        window.show()
        sys.exit(app.exec_())

You can see my output here: My Output Window

Does anyone know how to manipulate the picture inside .setStyleSheet to set it to the center of my window?

hexamax
  • 21
  • 5

1 Answers1

1

Okay, I think I've found a solution by myself...

My problem was that I didn't knew about the CSS part in setStyleSheet.

How to apply style sheet to a custom widget in PyQt

So my previous code needs to be changed like this:

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *

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

            self.setWindowTitle(f'Background Issue')
            self.setMinimumSize(1000, 800)
            self.setStyleSheet("""  background-image: url(max.png);
                                    background-repeat: no-repeat;
                                    background-position: center center;
            """)

    if __name__ == "__main__":
        app = QApplication(sys.argv)
        main_window = BackgroundIssue()
        main_window.show()
        sys.exit(app.exec_())

And the final view:

centered background image

hexamax
  • 21
  • 5
  • Please clarify what you actually did to solve your issue, since the question you link is only related in concept, not in practical solution. – musicamante Sep 22 '22 at 13:30
  • hey @musicamante, thanks for your response. I've edited my answer, I hope it's more clear now. – hexamax Sep 26 '22 at 08:00