马上要小初考了,做个倒计时提醒自己。
(需要下载pyqt5 下载指令:pip install pyqt5
如果下载慢用清华源:pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple)

(Countdown.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from sys import exit ,argv
from PyQt5.QtWidgets import QApplication,QMessageBox,QWidget,QLabel,QLCDNumber,QAction,QMenu,QInputDialog,QLineEdit,QDesktopWidget,QSystemTrayIcon
from PyQt5.QtCore import QTimer,Qt
from PyQt5.QtGui import QPixmap, QPainter,QPaintEvent,QIcon,QPainterPath
from datetime import datetime
from time import sleep

class myWindow(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.time_2 = '2023-06-17 08:00:00'
self.desktop = QApplication.desktop()
self.screenRect = self.desktop.screenGeometry()
self.screenheight = self.screenRect.height()
self.screenwidth = self.screenRect.width()

self.height_ = int(self.screenheight * 0.39)
self.width_ = int(self.screenwidth * 0.395)
self.resize(self.width_,self.height_)

self.setWindowFlags(Qt.SubWindow|Qt.FramelessWindowHint|Qt.FramelessWindowHint|Qt.WindowStaysOnBottomHint)
self.label = QLabel("距离小升初考大概还有:\n({})\n (天) (时) (分) (秒)".format(self.time_2),self)
self.labeltext = QLabel('',self)

screen = QDesktopWidget().screenGeometry()
size = self.geometry()
newLeft = (screen.width() - size.width())/2
self.move(int(newLeft),0)
font = self.label.font()
font.setPointSize(30)
font.setFamily("HYWenHei-55W")
fonttext = self.labeltext.font()
fonttext.setFamily("HYWenHei-55W")
fonttext.setPointSize(18)
self.label.setFont(font)
self.label.setGeometry(int(self.screenwidth * 0.05),0,int(self.screenwidth * 0.5),int(self.screenheight* 0.2))
self.labeltext.setFont(fonttext)
self.labeltext.setGeometry(int(self.screenwidth * 0.05),int(self.screenheight* 0.25),int(self.screenwidth * 0.5),int(self.screenheight* 0.2))


self.icon=QIcon('./R-C.ico')
Style = """
QMenu {
background-color: rgba(255, 255, 255, 230);
border: none;
border-radius: 4px;
}

QMenu::item {
border-radius: 4px;
padding: 8px 48px 8px 36px;
background-color: transparent;
}

QMenu::item:selected {
border-radius: 0px;
background-color: rgba(232, 232, 232, 232);
}

QMenu::item:disabled {
background-color: transparent;
}

QMenu::icon {
left: 15px;
}

QMenu::separator {
height: 1px;
background-color: rgb(232, 236, 243);
}
"""
self.tray = QSystemTrayIcon()
self.tray.setIcon(self.icon)
self.tray.setToolTip("Countdown")
showAction = QAction("显示", self, triggered = self.show)
hideAction = QAction("隐藏", self, triggered = self.hide)
updateAction = QAction("重新加载窗口", self, triggered = self.update)
openiniAction = QAction("重新加载配置文件", self, triggered = self.openini)
quitAction = QAction("退出", self, triggered = self.quit_app)
self.trayMenu = QMenu(self)
self.trayMenu.setAttribute(Qt.WA_TranslucentBackground)
self.trayMenu.setStyleSheet(Style)

self.trayMenu.setWindowFlags(self.trayMenu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
self.trayMenu.addAction(QAction("Countdown", self))
self.trayMenu.addAction(showAction)
self.trayMenu.addAction(hideAction)
self.trayMenu.addAction(updateAction)
self.trayMenu.addAction(openiniAction)
self.trayMenu.addAction(quitAction)
self.tray.setContextMenu(self.trayMenu)
self.tray.show()

self.lcdNumber = QLCDNumber(15,self)
self.lcdNumber.setLineWidth(0)
self.lcdNumber.setGeometry(int(self.screenwidth * 0.05),int(self.screenheight* 0.15),int(self.screenwidth * 0.3),int(self.screenheight* 0.2))
self.timer = QTimer(self)
self.lcdNumber.display('88:88:88:88 ')
self.timer.setInterval(1000)
self.timer.timeout.connect(self.change)
self.timer.start()
self.openini()
self.show()

def quit_app(self):
text , ok = QInputDialog.getText(self,"关闭窗口","请输入密码关闭窗口:",QLineEdit.Password)
if ok:
if text=='@royi123456':
self.tray==None
self.icon==None
self.close()
QMessageBox.information(self, "Countdown", "密码正确 点击OK关闭")
exit()
else:
QMessageBox.critical(self,"Countdown", "密码错误")
self.quit_app()

def openini(self):
f=None
with open('Countdown.ini','r',encoding='UTF-8') as f:
line=f.readlines()
self.color=line[0].strip('\n')
self.text=line[1].strip('\n')
self.bg=line[2].strip('\n')
self.hideshow=line[3].strip('\n')
self.label.setStyleSheet("color:{}".format(self.color))
self.labeltext.setStyleSheet("color:{}".format(self.color))
self.labeltext.setText(self.text)
self.lcdNumber.setStyleSheet("color:{}".format(self.color))

self.update()

def paintEvent(self, a0: QPaintEvent) -> None:
painter = QPainter(self)
pixmap = QPixmap('{}'.format(self.bg))
painter.drawPixmap(self.rect(), pixmap)
path = QPainterPath()
path.setFillRule(Qt.WindingFill)
def enterEvent(self, e):
if self.hideshow =='True':
for i in range(10):
self.setWindowOpacity (1-i/10)
sleep(0.01)
self.setWindowOpacity (0.005)
def leaveEvent(self, e):
if self.hideshow =='True':
for i in range(10):
self.setWindowOpacity (0+i/10)
sleep(0.01)
self.setWindowOpacity (1)
def change(self):
time_1 = datetime.now()
time_2_struct = datetime.strptime(self.time_2, "%Y-%m-%d %H:%M:%S")
seconds = (time_2_struct - time_1).seconds
string ='{:02d}:{:02d}:{:02d}:{:02d} '.format((time_2_struct - time_1).days,seconds//3600,seconds%3600//60,seconds%60)
self.lcdNumber.display(string)

if __name__ == '__main__':
app=QApplication(argv)
window = myWindow()
n= app.exec()
exit(n)

上面是源代码
运行后会报错:

1
2
3
4
5
6
7
8
Traceback (most recent call last):
File "xxx\Countdown.py", line 161, in <module>
window = myWindow()
File "xxx\Countdown.py", line 103, in __init__
self.openini()
File "xxx\Countdown.py", line 121, in openini
with open('Countdown.ini','r',encoding='UTF-8') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'Countdown.ini'

翻译一下就是找不到’Countdown.ini’
所以要在’Countdown.py’同一目录下添加’Countdown.ini’

(Countdown.ini)

1
2
3
4
#ffffff
星光不问赶路人,时间不负有心人。
./xxx.jpg
False

可以按以下方法配置
(#ffffff)是文字颜色
(星光不问赶路人,时间不负有心人。)是祝福语
(./xxx.jpg)背景图片
(False)鼠标放上去是否隐藏

图片一定要选择!!!

后面点击下面链接下载字体文件

点击下载
(好慢… 在后面下载压缩包吧)

就可以运行了

源代码压缩包点击下载