How to make the image occupy 100% of the screen. pyqt5 -python

Asked

Viewed 1,017 times

0

I have an image and I need it to occupy 100%, will be the background of my program, how to proceed?

from PyQt5 import QtWidgets, QtGui
import sys

class Hello_World(QtWidgets.QWidget):
    def __init__(self):
        super(Hello_World, self).__init__()
        self.setWindowTitle("Olá mundo!")
        self.setGeometry(200,200,700,500)
        self.setStyleSheet("Background-Color: #d9b3ff;")

        self.texto = QtWidgets.QLabel(self)
        self.texto.setPixmap(QtGui.QPixmap("Imagens/fundo.jpg"))

if __name__=="__main__":
    app = QtWidgets.QApplication(sys.argv)
    janela = Hello_World()
    janela.show()
    sys.exit(app.exec_())

1 answer

1

You can scale the image, and use it as the background palette:

   oImage = QImage("fundo.png")
   sImage = oImage.scaled(QSize(700,500)) #use o tamanho do fundo
   palette = QPalette()
   palette.setBrush(10, QBrush(sImage))
   # esse 10 acima é a propriedade Window role, veja o manual da Qt
   self.setPalette(palette)

This avoids the need to "improvise" a label background, and passes the responsibility of drawing to the right place (the "painting" of the background).

If you want the size to go with the resize window, just change the oImage.scaled(QSize(700,500)) for oImage.scaled(self.size()), or preferably do so on SIGNAL("resized()") automatically.

http://doc.qt.io/archives/qt-4.8/qwidget.html#size-prop

  • I forgot to comment, but I wanted to adjust this background to grow when I click the maximize button.. thanks for the answer

  • It’s the same thing, only you instead of by fixed size, use the window size. I updated the answer.

  • looks good, I’ll test, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.