|
| 1 | +''' |
| 2 | +Author: LetMeFly |
| 3 | +Date: 2022-09-13 09:17:12 |
| 4 | +LastEditors: LetMeFly |
| 5 | +LastEditTime: 2022-09-13 11:08:20 |
| 6 | +''' |
| 7 | +import sys |
| 8 | +import time |
| 9 | +import webbrowser |
| 10 | +from threading import Thread |
| 11 | +from PyQt5 import QtCore, QtWidgets |
| 12 | +from PyQt5.QtCore import QPoint, QSize, Qt |
| 13 | +from PyQt5.QtGui import QMouseEvent, QCursor, QPalette, QColor |
| 14 | +from PyQt5.QtWidgets import QApplication, QMenu, QMessageBox, QWidget, qApp |
| 15 | + |
| 16 | + |
| 17 | +class Main(QWidget): |
| 18 | + _lastTime = None |
| 19 | + _nowCount = 0 |
| 20 | + _timeIsCounting = False |
| 21 | + |
| 22 | + # 移动位置 |
| 23 | + _leftButtonDownIng = False |
| 24 | + _moveStartPosition = None |
| 25 | + |
| 26 | + # About Me |
| 27 | + about = "Small desktop timer\nMade by: LetMeFly" # 小小桌面计时器 |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + super().__init__() |
| 31 | + self.setFixedSize(QSize(300, 70)) |
| 32 | + self.setWindowFlags(Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | Qt.Tool) |
| 33 | + self.setWindowOpacity(0.8) |
| 34 | + |
| 35 | + self.label = QtWidgets.QLabel(self) |
| 36 | + self.label.setGeometry(self.rect()) |
| 37 | + self.label.setStyleSheet("font: 75 20pt \"Adobe Arabic\";color:rgb(0, 122, 204)") |
| 38 | + self.label.setAlignment(QtCore.Qt.AlignCenter) |
| 39 | + |
| 40 | + self.pal = QPalette() |
| 41 | + self.pal.setColor(QPalette.Background, QColor(255, 228, 255, 50)) |
| 42 | + self.setPalette(self.pal) |
| 43 | + |
| 44 | + self.timer = QtCore.QTimer(self) |
| 45 | + self.timer.start(50) |
| 46 | + self.timer.timeout.connect(self.run) |
| 47 | + |
| 48 | + self.setCursor(QCursor(Qt.PointingHandCursor)) |
| 49 | + |
| 50 | + self.show() |
| 51 | + |
| 52 | + def run(self): |
| 53 | + Thread(target=self.setTime, daemon=True).start() |
| 54 | + |
| 55 | + def setTime(self): |
| 56 | + def strfTime(count: int) -> str: |
| 57 | + H = int(count) // 3600 |
| 58 | + M = int(count) % 3600 // 60 |
| 59 | + S = int(count) % 60 |
| 60 | + mS = int(count * 100) % 100 |
| 61 | + return "{:02d}H:{:02d}M:{:02d}S:{:02d}".format(H, M, S, mS) |
| 62 | + |
| 63 | + def getTime() -> int: |
| 64 | + if self._timeIsCounting: |
| 65 | + nowTime = time.time() |
| 66 | + self._nowCount += nowTime - self._lastTime |
| 67 | + self._lastTime = nowTime |
| 68 | + return self._nowCount |
| 69 | + |
| 70 | + self.label.setText(strfTime(getTime())) |
| 71 | + |
| 72 | + def mouseMoveEvent(self, e: QMouseEvent): |
| 73 | + if self._leftButtonDownIng: |
| 74 | + self._endPos = e.pos() - self._moveStartPosition |
| 75 | + self.move(self.pos() + self._endPos) |
| 76 | + |
| 77 | + def mousePressEvent(self, e: QMouseEvent): |
| 78 | + if e.button() == Qt.LeftButton: |
| 79 | + self._leftButtonDownIng = True |
| 80 | + self._moveStartPosition = QPoint(e.x(), e.y()) |
| 81 | + |
| 82 | + def mouseReleaseEvent(self, e: QMouseEvent): |
| 83 | + if e.button() == Qt.LeftButton: |
| 84 | + self._leftButtonDownIng = False |
| 85 | + self._moveStartPosition = None |
| 86 | + if e.button() == Qt.RightButton: |
| 87 | + menu = QMenu(self) |
| 88 | + clearAction = None |
| 89 | + if self._timeIsCounting: |
| 90 | + startpauseAction = menu.addAction("Pause") |
| 91 | + else: |
| 92 | + if self._nowCount: |
| 93 | + startpauseAction = menu.addAction("Resume") |
| 94 | + clearAction = menu.addAction("Clear") |
| 95 | + else: |
| 96 | + startpauseAction = menu.addAction("Start") |
| 97 | + menu.addSeparator() |
| 98 | + aboutAction = menu.addAction("About") |
| 99 | + quitAction = menu.addAction("Exit") |
| 100 | + action = menu.exec_(self.mapToGlobal(e.pos())) |
| 101 | + if action == startpauseAction: |
| 102 | + if self._timeIsCounting: |
| 103 | + self._timeIsCounting = False |
| 104 | + else: |
| 105 | + self._timeIsCounting = True |
| 106 | + self._lastTime = time.time() |
| 107 | + if action == clearAction: |
| 108 | + self._nowCount = 0 |
| 109 | + if action == quitAction: |
| 110 | + qApp.quit() |
| 111 | + if action == aboutAction: |
| 112 | + selected = QMessageBox.question(self, "About", self.about, QMessageBox.Yes | QMessageBox.Cancel) |
| 113 | + if selected == QMessageBox.Yes: |
| 114 | + webbrowser.open('https://letmefly.xyz/?from=MyTimer', new=0, autoraise=True) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + app = QApplication(sys.argv) |
| 119 | + ex = Main() |
| 120 | + sys.exit(app.exec_()) |
| 121 | + |
0 commit comments