Resumo : neste tutorial, você aprenderá como usar o QLabel
widget PyQt para exibir texto ou imagem.
Introdução ao widget PyQt QLabel
A QLabel
classe 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 QLabel
widget do PyQt6.QtWidgets
módulo:
from PyQt6.QtWidgets import QLabel
Linguagem de código: Python ( python )
Segundo, crie uma nova instância da QLabel
classe:
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 QLabel
widget após criá QLabel
-lo:
label = QLabel()
label.setText('This is QLabel widget')
Linguagem de código: Python ( python )
Para obter o texto do
widget, você chama o QLabel
()text()
método:
label.text()
Linguagem de código: Python ( python )
Para limpar o texto de um QLabel
widget, 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 QLabel
widget:
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:
Usando o widget PyQt QLabel para exibir uma imagem
Para exibir uma imagem usando o QLabel
widget, siga as seguintes etapas:
Primeiro, importe QPixmap
do PyQt6.QtGui
módulo:
from PyQt6.QtGui import QPixmap
Linguagem de código: Python ( python )
Segundo, crie um novo QPixmap
widget com um caminho para um arquivo de imagem:
pixmap = QPixmap('python-logo.svg')
Linguagem de código: Python ( python )
Observe que o python-logo.svg
arquivo deve estar no mesmo diretório do programa python
Terceiro, crie um QLabel
widget 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 QLabel
widget:
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 QPixmap
widget 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 QMovie
do PyQt6.QtGui
módulo:
from PyQt6.QtGui import QMovie
Linguagem de código: Python ( python )
Segundo, crie um QMovie
objeto com um caminho para o arquivo GIF:
movie = QMovie('python.gif')
Linguagem de código: Python ( python )
Terceiro, crie um novo QLabel
widget:
label = QLabel(self)
Linguagem de código: Python ( python )
Quarto, configure o filme para o QLabel
widget chamando o setMovie()
método:
label.setMovie(movie)
Linguagem de código: Python ( python )
Por fim, chame o start()
método do QMovie
objeto 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
QLabel
para exibir um texto ou imagem incluindo uma imagem animada.