as per image to resize in Fullscreen Pyqt5

Asked

Viewed 76 times

1

I’m starting as an IT and I found this perfect library that is pyqt5, but there are some things that I can’t fully do because the method I use seems to not work. Instead of creating a screen just by code, or creating in Qtdesigner and passing it to Python, I use both at the same time, I edit in Qtdesigner and import by "uic.Loadui()". I wanted to put an image like Background on a full screen, but when I put the image in the centralwidget, it either gets too big, or it gets cropped, plus it doesn’t just look like wallpaper but for all the things I put in there, a label, a Qlineedit etc. how can I make it resize itself to full screen on a PC and not stay on all objects?

an example test:

from PyQt5 import uic,QtWidgets
#app
app=QtWidgets.QApplication([])
Tela_qualquer = uic.loadUi('teste.ui')
Tela_qualquer.setStyleSheet("background-image: url(df.jpg); background-repeat: no-repeat; background-position: center")
Tela_qualquer.show()
app.exec()

The screen with a button and a Qlabel and the photo cropped: Imagem cortada com botão e Qlabel borrado

The whole picture: A imagem inteira

1 answer

0

Hello, you have to put another layout that is above the central_widget, and it is in this layout that you put the components. First place the background image in the central_widget:

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


class TelaPrincipal(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(TelaPrincipal, self).__init__(*args, **kwargs)
        self.setWindowTitle('Tela Principal')
        self.setGeometry(100, 200, 600, 300)
        self.setMinimumSize(600, 300)

        image_path = r'df.jpg'

        # label para colocar a imagem
        central_widget = QLabel()

        pix_map = QPixmap()
        pix_map.load(image_path)
        central_widget.setPixmap(pix_map)

        # redimensionar a imagem
        central_widget.setScaledContents(True)

        # usar o label como central widget
        self.setCentralWidget(central_widget)

        # criar o layout do central widget para colocar outros widgets por cima
        self.layout = QVBoxLayout(central_widget)

Then you put a widget on top where it takes the components:

        # criar o widget e o respectivo layout para receber os componentes
        widget_1 = QWidget()
        widget_1_layout = QVBoxLayout(widget_1)

        # criar os componentes
        label_1 = QLabel('label 1')
        label_1.setStyleSheet('color: red')

        label_2 = QLabel('label 2')
        label_2.setStyleSheet('color: red')

        btn_1 = QPushButton('btn 1')
        btn_2 = QPushButton('btn 2')

        # colocar o widget_1 no layout do central widget
        self.layout.addWidget(widget_1)

        # colocar os componentes no layout do widget_1
        widget_1_layout.addWidget(label_1)
        widget_1_layout.addWidget(btn_1)
        widget_1_layout.addWidget(label_2)
        widget_1_layout.addWidget(btn_2)

        self.setLayout(self.layout)


def main():
    app = QApplication(sys.argv)
    root = TelaPrincipal()
    root.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Browser other questions tagged

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