使用pyqt6写一个更新qlabel的例子
时间: 2024-05-01 07:20:22 浏览: 30
好的,以下是一个使用 PyQT6 更新 QLabel 的示例:
```python
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton
from PyQt6.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建 QLabel 和 QPushButton
self.label = QLabel('Hello, world!', self)
self.button = QPushButton('Click me!', self)
self.button.clicked.connect(self.update_label)
def update_label(self):
# 更新 QLabel 的文本
self.label.setText('Button clicked!')
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
```
在这个例子中,我们创建了一个 `MainWindow` 类,它包含一个 QLabel 和一个 QPushButton。当用户点击 QPushButton 时,我们调用 `update_label` 方法来更新 QLabel 的文本。
在 `update_label` 方法中,我们使用 `setText` 方法来更新 QLabel 的文本。
最后,在主程序中,我们创建了一个 QApplication 实例和一个 MainWindow 实例,并执行应用程序循环。