PyQt QLabel

Resumo : neste tutorial, você aprenderá como usar o QLabelwidget PyQt para exibir texto ou imagem.

Introdução ao widget PyQt QLabel

A QLabelclasse permite criar um widget de rótulo que exibe texto, uma imagem ou uma imagem animada (GIF).

Para criar um widget de rótulo, siga estas etapas:

Primeiro, importe o QLabelwidget do PyQt6.QtWidgetsmódulo:

from PyQt6.QtWidgets import QLabelLinguagem de código:  Python  ( python )

Segundo, crie uma nova instância da QLabelclasse:

label = QLabel('This is QLabel widget')Linguagem de código:  Python  ( python )

Nesta sintaxe, você passa uma string que deseja exibir para o arquivo QLabel.

Além disso, você pode usar o setText()método para definir um texto para o QLabelwidget após criá QLabel-lo:

label = QLabel()
label.setText('This is QLabel widget')Linguagem de código:  Python  ( python )

Para obter o texto do QLabel()widget, você chama o text()método:

label.text()Linguagem de código:  Python  ( python )

Para limpar o texto de um QLabelwidget, você usa o clear()método:

label.clear()Linguagem de código:  Python  ( python )

Exemplo de widget PyQt QLabel

O programa a seguir mostra uma janela que exibe um QLabelwidget:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt6.QtGui import QFont


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt Label Widget')
        self.setGeometry(100, 100, 320, 210)

        # create a QLabel widget
        label = QLabel('This is a QLabel widget')

        # place the widget on the window
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        # show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create the main window
    window = MainWindow()

    # start the event loop
    sys.exit(app.exec())Linguagem de código:  Python  ( python )

Saída:

Exemplo PyQt QLabel

Usando o widget PyQt QLabel para exibir uma imagem

Para exibir uma imagem usando o QLabelwidget, siga as seguintes etapas:

Primeiro, importe QPixmapdo PyQt6.QtGuimódulo:

from PyQt6.QtGui import QPixmapLinguagem de código:  Python  ( python )

Segundo, crie um novo QPixmapwidget com um caminho para um arquivo de imagem:

pixmap = QPixmap('python-logo.svg')Linguagem de código:  Python  ( python )

Observe que o python-logo.svgarquivo deve estar no mesmo diretório do programa python

Terceiro, crie um QLabelwidget e chame o setPixmap()método para exibir a imagem:

label = QLabel()
label.setPixmap(pixmap)Linguagem de código:  Python  ( python )

O programa a seguir mostra como exibir uma imagem usando um QLabelwidget:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt6.QtGui import QPixmap


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt Label Widget')
        self.setGeometry(100, 100, 320, 210)

        label = QLabel()
        pixmap = QPixmap('python-logo.png')
        label.setPixmap(pixmap)

        # place the widget on the window
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        # show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create the main window
    window = MainWindow()

    # start the event loop
    sys.exit(app.exec())Linguagem de código:  Python  ( python )

Saída:

O QPixmapwidget suporta os seguintes formatos de imagem populares, incluindo bmp, gif, jpg, jpeg, png, etc.

Usando o widget PyQt QLabel para exibir uma imagem animada

Para criar um filme, você pode seguir estas etapas:

Primeiro, importe QMoviedo PyQt6.QtGuimódulo:

from PyQt6.QtGui import QMovieLinguagem de código:  Python  ( python )

Segundo, crie um QMovieobjeto com um caminho para o arquivo GIF:

movie = QMovie('python.gif')Linguagem de código:  Python  ( python )

Terceiro, crie um novo QLabelwidget:

label = QLabel(self)Linguagem de código:  Python  ( python )

Quarto, configure o filme para o QLabelwidget chamando o setMovie()método:

label.setMovie(movie)Linguagem de código:  Python  ( python )

Por fim, chame o start()método do QMovieobjeto para mostrar o filme:

movie.start()Linguagem de código:  Python  ( python )

O programa a seguir mostra como exibir um filme usando os widgets QLabel& QMovie:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt6.QtGui import QMovie


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt QLabel Widget')
        self.setGeometry(100, 100, 320, 210)        

        label = QLabel()
        movie = QMovie('python.gif')
        label.setMovie(movie)
        movie.start()

        # place the widget on the window
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

        # show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create the main window
    window = MainWindow()

    # start the event loop
    sys.exit(app.exec())Linguagem de código:  Python  ( python )

Resumo

  • Use um widget PyQt QLabelpara exibir um texto ou imagem incluindo uma imagem animada.

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *