等着下班回家,太煎熬了,写个倒计时,注释是不可能有注释的,只想下班
from PyQt5 import QtWidgets,QtCore,QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
import time
class RefreshWidget(QWidget):
def __init__(self,parent=None):
super(RefreshWidget, self).__init__(parent)
layout=QVBoxLayout()
self.setWindowTitle("回家倒计时")
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
pushButton = QPushButton("开始")
pushButton.clicked.connect(self.call_back_btn_func)
layout.addWidget(self.label)
layout.addWidget(pushButton)
self.setLayout(layout)
self.resize(400, 300)
# 回家时间
self.end_time = '2022-01-28 19:00:00'
def call_back_btn_func(self):
while True:
h, m, s = self.get_time()
self.label.setText(f'回家倒计时:{h}:{m}:{s}')
# 实时刷新
app.processEvents()
time.sleep(1)
if h==0 and m==0 and s==0:break
def get_time(self):
time_arr = time.strptime(self.end_time, "%Y-%m-%d %H:%M:%S")
ts = int(time.mktime(time_arr))
a = int(ts - time.time())
h = int(a / 3600)
m = int((a - h * 3600) / 60)
s = a - h * 3600 - m * 60
# print(f'toatls:{a},h:{h},m:{m},s:{s}')
return h, m, s
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window=RefreshWidget()
window.show()
sys.exit(app.exec())