T
T
ti_zh_vrach2021-08-01 12:47:46
PyQt
ti_zh_vrach, 2021-08-01 12:47:46

How to scale a background image with aspect ratio with PyQt?

Good afternoon!
While it turned out to be done without maintaining the aspect ratio.

class Interface(QMainWindow):

    def __init__(self):
        super().__init__()
        self.main_window()

    def main_window(self):
        QToolTip.setFont(QFont('TimesNewRoman', 12))

        start_button = QPushButton('Start', self)
        start_button.setFlat(True)
        start_button.setStyleSheet("QPushButton {background-color: rgba(255, 0, 0, 0);"
                                   "border-radius: 100px;}")
        start_button.resize(200, 200)
        start_button.move(100, 100)
        start_button.setFont(QFont('Calibri', 42))
        start_button.clicked.connect(run_convertor)
        start_button.setToolTip('Button tip')

        self.resize(1000, 500)
        self.center()
        self.setWindowTitle('jpgTabaki')
        self.setWindowIcon(QIcon('icon.png'))
        self.setToolTip('Window tip')
        self.setStyleSheet("QMainWindow {border-image: url(x_pic.png);"
                           "background-repeat: no-repeat; background-position: center;}")
        self.show()

    def center(self):
        q_screen = self.frameGeometry()
        q_resolution = QDesktopWidget().availableGeometry().center()
        q_screen.moveCenter(q_resolution)
        self.move(q_screen.topLeft())

When I searched in English, I saw solutions with QWidget instead of QMainWindow, but my background disappeared altogether. Probably I didn't understand something. When I searched in Russian, I generally got some rubbish on the topic of analytics and conferences.

How can you keep the aspect ratio while scaling?

And more interesting. If you remove "background-repeat: no-repeat; background-position: center;", then the background also disappears. Why is that?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question