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