T
T
ti_zh_vrach2021-07-24 14:44:42
PyQt
ti_zh_vrach, 2021-07-24 14:44:42

Why is the background on the button and not on the main window (PyQt5)?

Good afternoon!
I'm trying to insert a background image into the main window of a windows application. I am using PyQt5 from Riverbank.
I inserted the picture like this:

self.setStyleSheet("background-image: url(x_pic.png);"
                   "background-repeat: no-repeat;background-position: center;")

The GUI did this:

class Interface(QWidget):

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

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

        start_button = QPushButton('Start', self)
        start_button.clicked.connect(run_convertor)
        start_button.resize(start_button.sizeHint())
        start_button.move(100, 100)
        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("background-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())


The background appears on the button, not the window. How to make the background not for the button, but for the main window?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ti_zh_vrach, 2021-07-24
@ti_zh_vrach

Replaced QWidget with QMainWindow. At the same time, I made the button round and invisible (under the picture it is necessary).

The code

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.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')
        start_button.setStyleSheet("QPushButton {background-color: rgba(255, 0, 0, 0);"
                                   "border-radius: 100px;}")

        self.resize(1000, 500)
        self.center()
        self.setWindowTitle('jpgTabaki')
        self.setWindowIcon(QIcon('icon.png'))
        self.setToolTip('Window tip')
        self.setStyleSheet("QMainWindow {background-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())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question