📅  最后修改于: 2023-12-03 15:07:36.598000             🧑  作者: Mango
大家好,圣诞节又快要到了,为了让大家更好地度过这个节日,我们特意为大家准备了一个圣诞节倒计时程序。
本程序使用了以下技术:
以下是程序运行时的界面截图:
以下是程序的主要代码,详细代码请查看附件:
class Countdown(QThread):
def __init__(self, parent=None):
super().__init__(parent)
self.date = QDate.currentDate()
self.time = QTime(23, 59, 59)
def setEndDateTime(self, dateTime):
self.date = dateTime.date()
self.time = dateTime.time()
def run(self):
while True:
now = QDateTime.currentDateTime()
remaining = now.secsTo(QDateTime(self.date, self.time))
if remaining <= 0:
break
days = remaining // 86400
hours = (remaining % 86400) // 3600
minutes = (remaining % 3600) // 60
seconds = remaining % 60
self.remainingTime.emit(days, hours, minutes, seconds)
time.sleep(1)
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()
def initUI(self):
# 界面布局
self.label = QLabel(self.tr("距离圣诞节还有:"))
self.days_label = QLabel("")
self.days_label.setObjectName("DaysLabel")
self.hours_label = QLabel("")
self.hours_label.setObjectName("HoursLabel")
self.minutes_label = QLabel("")
self.minutes_label.setObjectName("MinutesLabel")
self.seconds_label = QLabel("")
self.seconds_label.setObjectName("SecondsLabel")
layout = QHBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.days_label)
layout.addWidget(self.hours_label)
layout.addWidget(self.minutes_label)
layout.addWidget(self.seconds_label)
# 连接倒计时线程的信号
self.countdown = Countdown()
self.countdown.remainingTime.connect(self.updateRemainingTime)
def updateRemainingTime(self, days, hours, minutes, seconds):
# 更新界面
self.days_label.setText("{}天".format(days))
self.hours_label.setText("{}小时".format(hours))
self.minutes_label.setText("{}分钟".format(minutes))
self.seconds_label.setText("{}秒钟".format(seconds))
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置日期时间
endDateTime = QDateTime(QDate(2021, 12, 25), QTime(23, 59, 59))
# 创建窗口
widget = Widget()
widget.countdown.setEndDateTime(endDateTime)
# 显示窗口
widget.show()
sys.exit(app.exec_())
以上是本程序的介绍和代码片段,谢谢大家!