from PyQt5.QtCore import QTimer, QDateTime, QPropertyAnimation, QEasingCurve, pyqtProperty
from PyQt5.Qt import *
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit, QPushButton, QApplication, QDialog
import sys
import time
class AnimatedLabel(QLabel):
def __init__(self, text="", parent=None):
# def __init__(self, text=""):
super().__init__(text, parent)
# super().__init__(text)
self.setFontSize(25) # 设置初始字体大小
self._font_size = 25 # 内部变量用于动画控制
@pyqtProperty(int)
def fontSize(self):
return self._font_size
@fontSize.setter
def fontSize(self, value):
self._font_size = value
self.setFontSize(value)
def setFontSize(self, size):
font = self.font()
font.setPointSize(size)
self.setFont(font)
class Mainwindow(QMainWindow):
def __init__(self):
super(Mainwindow, self).__init__()
self.start_time = None
self.resize(440, 430)
# self.setWindowTitle('实时薪资计数器')
self.setWindowFlag(Qt.FramelessWindowHint)
self.setStyleSheet("font-family: Microsoft YaHei UI;")
self.label_salary = QLabel(self)
self.label_salary.setText('月薪')
self.label_salary.setGeometry(20, 20, 100, 20)
self.lineedit_salary = QLineEdit(self)
self.lineedit_salary.setGeometry(20, 60, 200, 25)
self.label_workdays = QLabel(self)
self.label_workdays.setGeometry(20, 105, 200, 20)
self.label_workdays.setText('每月工作天数')
self.lineedit_workdays = QLineEdit(self)
self.lineedit_workdays.setGeometry(20, 145, 200, 25)
self.label_workhours = QLabel(self)
self.label_workhours.setGeometry(20, 185, 200, 20)
self.label_workhours.setText('每天工作小时数')
self.lineedit_workhours = QLineEdit(self)
self.lineedit_workhours.setGeometry(20, 225, 200, 25)
self.button_start = QPushButton(self)
self.button_start.setGeometry(70, 265, 100, 30)
self.button_start.setStyleSheet("""
QPushButton {
background-color: #00BFFF;
border-radius: 5px;
color: white;
font-family: Microsoft YaHei UI;
}
""")
# self.button_start.setStyleSheet("background-color: #00BFFF;")
self.button_start.setText('开始计算')
self.button_start.clicked.connect(self.calculate_salary)
self.button_start.clicked.connect(self.start_timer)
# self.label_make_money = QLabel(self)
# self.label_make_money.setText('您已赚到:')
# self.label_make_money.setGeometry(270, 20, 100, 20)
self.label_hour = QLabel(self)
self.label_hour.setText('每小时:0.00元')
self.label_hour.setGeometry(20, 305, 200, 20)
self.label_minute = QLabel(self)
self.label_minute.setText('每分钟:0.00元')
self.label_minute.setGeometry(20, 345, 200, 20)
self.label_second = QLabel(self)
self.label_second.setText('每秒钟:0.00元')
self.label_second.setGeometry(20, 385, 200, 20)
self.spend_time = QLabel(self)
self.spend_time.setText('已用时:')
self.spend_time.setGeometry(290, 140, 100, 20)
self.label_pic_time = QLabel(self)
pixmap_time = QPixmap("C:\\Users\\14320\\Downloads\\游戏时间.png")
self.label_pic_time.setGeometry(290, 180, 20, 20)
self.label_pic_time.setPixmap(pixmap_time)
self.label_pic_time.setScaledContents(True)
self.label_spend_time = QLabel(self)
self.label_spend_time.setGeometry(320, 180, 200, 20)
self.label_spend_time.setStyleSheet("""
QLabel {
font-family: Microsoft YaHei UI;
font-size: 20px;
color: #8AC8FF;
font-weight: bold;
}
""")
self.label_spend_time.setText('00:00:00')
self.label_make = QLabel(self)
self.label_make.setGeometry(290, 240, 100, 20)
self.label_make.setText('你已经赚到:')
self.label_pic = QLabel(self)
pixmap_money = QPixmap("C:\\Users\\14320\\Downloads\\钱袋.png")
self.label_pic.setGeometry(290, 280, 30, 40)
self.label_pic.setPixmap(pixmap_money)
self.label_pic.setScaledContents(True)
self.label_money = AnimatedLabel('0.00元', self)
self.label_money.setGeometry(330, 280, 100, 40)
self.label_money.setStyleSheet("""
QLabel {
font-family: Microsoft YaHei UI;
font-size: 25px;
color: #FF6A48;
font-weight: bold;
}
""")
# self.label_money.setText('0.00元')
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_label)
def start_timer(self):
if not self.start_time: # 如果计时未开始
self.start_time = QDateTime.currentDateTime() # 记录计时起点
self.timer.start(1000) # 每隔1秒触发一次
self.button_start.setText("停止计时") # 更新按钮文本
else:
self.stop_timer() # 如果计时已开始,则停止计时
def stop_timer(self):
self.timer.stop() # 停止计时器
self.start_time = None # 重置计时起点
self.label_spend_time.setText("计时已停止") # 更新标签文本
self.button_start.setText("开始计算") # 更新按钮文本
self.label_money.setText('0.00元')
self.label_hour.setText('每小时:0.00元')
self.label_minute.setText('每分钟:0.00元')
self.label_second.setText('每秒钟:0.00元')
def update_label(self):
if self.start_time:
elapsed_time = QDateTime.currentDateTime().toSecsSinceEpoch() - self.start_time.toSecsSinceEpoch()
elapsed_second = elapsed_time % 60
elapsed_minute = elapsed_time // 60 % 60
elapsed_hour = elapsed_time // 3600 % 24
self.label_spend_time.setText(f"{elapsed_hour:02d}:{elapsed_minute:02d}:{elapsed_second:02d}")
money = elapsed_time * self.second
self.label_money.setText(f'{money:.2f}元')
animation = QPropertyAnimation(self.label_money, b"fontSize", self)
animation.setDuration(300) # 动画持续时间
animation.setStartValue(25) # 起始字体大小
animation.setEndValue(50) # 结束字体大小
animation.setEasingCurve(QEasingCurve.OutQuad) # 动画曲线类型
# 在动画结束时恢复字体大小
animation.finished.connect(lambda: self.label_money.setFontSize(25))
animation.start()
def calculate_salary(self):
self.time_1 = time.time()
self.hour = float(self.lineedit_salary.text()) / float(self.lineedit_workdays.text()) / float(self.lineedit_workhours.text())
self.minute = self.hour / 60
self.second = self.minute / 60
self.label_hour.setText(f'每小时:{self.hour:.2f}元')
self.label_minute.setText(f'每分钟:{self.minute:.2f}元')
self.label_second.setText(f'每秒钟:{self.second:.2f}元')
# self.label_spend_time.setText()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Mainwindow()
win.show()
sys.exit(app.exec())
修改一下