pyqt状态栏显示时间
时间: 2023-08-20 10:04:05 浏览: 135
要在PyQt中显示时间,可以将QLabel控件添加到状态栏中,并使用QTimer更新标签中显示的时间。以下是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import QTimer, QTime
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置状态栏
self.statusBar().showMessage("Ready")
self.label = QLabel()
self.statusBar().addPermanentWidget(self.label)
# 创建定时器
timer = QTimer(self)
timer.timeout.connect(self.update_time)
timer.start(1000)
def update_time(self):
# 获取当前时间并更新标签
current_time = QTime.currentTime().toString('hh:mm:ss')
self.label.setText("当前时间:" + current_time)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
该代码创建了一个MainWindow类,其中状态栏包含一个QLabel控件,用于显示当前时间。QTimer用于每秒更新时间,以便在状态栏中显示最新的时间。
阅读全文
相关推荐


















