pyqt5倒计时120s代码
时间: 2023-11-12 17:51:32 浏览: 53
好的,以下是 Pyqt5 实现 120 秒倒计时的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import QTimer, Qt
class CountDown(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.updateTimer)
self.timer.start(1000)
self.remainingTime = 120
def initUI(self):
self.setGeometry(400, 400, 300, 200)
self.setWindowTitle('120 Second Countdown')
self.timeLabel = QLabel(str(self.remainingTime), self)
self.timeLabel.setAlignment(Qt.AlignCenter)
self.timeLabel.setStyleSheet('font-size: 60px; color: red;')
vbox = QVBoxLayout()
vbox.addWidget(self.timeLabel)
self.setLayout(vbox)
def updateTimer(self):
self.remainingTime -= 1
self.timeLabel.setText(str(self.remainingTime))
if self.remainingTime == 0:
self.timer.stop()
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
countdown = CountDown()
countdown.show()
sys.exit(app.exec_())
```
希望能对您有所帮助。如果您还有其他问题,欢迎随时问我。