pythonpyqtpyqt5qlabelqlayout

PyQt5 I can't see background picture when I added layouts


import sys
from PyQt5 import QtWidgets,QtGui


def Window():
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()

    window.setWindowTitle("GORKIDEV Application")
    window.setWindowIcon(QtGui.QIcon("telefon.png"))
    window.setGeometry(700,300,488,499)

    background_picture = QtWidgets.QLabel(window)
    background_picture.setPixmap(QtGui.QPixmap("telefon.png"))

    okay = QtWidgets.QPushButton("Okay")
    cancel = QtWidgets.QPushButton("Cancel")

    h_box = QtWidgets.QHBoxLayout()

    h_box.addStretch()
    h_box.addWidget(okay)
    h_box.addWidget(cancel)

    v_box = QtWidgets.QVBoxLayout()
    v_box.addStretch()
    v_box.addLayout(h_box)

    window.setLayout(v_box)

    window.show()
    sys.exit(app.exec_())

Window()


Hello guys, I just added background picture this window and after that I added 2 layout which include just two widget.
When I execute codes I just see a bit part of picture on the up left side.

Where is the problem? Why I don't see picture clearly?

This is the output of the code.

enter image description here


Solution

  • Insert background_picture into layout.

    import sys
    from PyQt5 import QtWidgets, QtGui,  QtCore
    
    
    class MainWindow(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
    
            self.setWindowTitle("GORKIDEV Application")
            self.setWindowIcon(QtGui.QIcon("Ok.png"))
    # ?      self.setGeometry(700, 300, 488, 499)
            self.resize(488, 499)
    
            background_picture = QtWidgets.QLabel(self)
            background_picture.setPixmap(QtGui.QPixmap("lena2.png"))
    
            okay = QtWidgets.QPushButton("Okay")
            cancel = QtWidgets.QPushButton("Cancel")
    
            h_box = QtWidgets.QHBoxLayout()
            h_box.addStretch()
            h_box.addWidget(okay)
            h_box.addWidget(cancel)
    
            v_box = QtWidgets.QVBoxLayout(self)
            v_box.addWidget(background_picture, alignment=QtCore.Qt.AlignCenter) # !!! +++
            v_box.addStretch()
            v_box.addLayout(h_box)
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

    enter image description here