forked from CHNZYX/Auto_Simulated_Universe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotif.py
87 lines (75 loc) · 2.35 KB
/
notif.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
import os
import ctypes
import time
from PIL import Image
from pystray import Icon, MenuItem as item
import win32con, win32api
import threading
from winotify import Notification
def notif(title,msg):
Notification(app_id="椰羊自动化",title=title,msg=msg,icon=os.getcwd() + "\\imgs\\icon.png").show()
def show_notification(icon, item):
ctypes.windll.user32.MessageBoxW(0, "程序已在运行!", "提示", 0x40)
icon.stop()
def exit_program(icon, item):
icon.stop()
os._exit(0)
def maopao(icon=None, item=None):
file_name = 'logs/notif.txt'
cnt='0'
tm=None
if os.path.exists(file_name):
with open(file_name, 'r', encoding="utf-8",errors='ignore') as file:
s=file.readlines()
cnt=s[0].strip('\n')
try:
tm=s[3].strip('\n')
except:
pass
if tm is None:
tm = str(time.time())
os.makedirs('logs',exist_ok=1)
with open(file_name, 'w', encoding="utf-8") as file:
file.write(f"{cnt}\n喵\n计数:{cnt}\n{tm}")
def notify():
file_name = 'logs/notif.txt'
if not os.path.exists(file_name):
with open(file_name, 'w', encoding="utf-8") as file:
file.write("0")
last = os.path.getmtime(file_name)
while 1:
time.sleep(0.5)
if last != os.path.getmtime(file_name):
with open(file_name,'r', encoding="utf-8",errors='ignore') as fh:
s=fh.readlines()
if len(s)>=3:
notif(s[1].strip('\n'),s[2].strip('\n'))
last = os.path.getmtime(file_name)
def main():
# 检测程序是否已经在运行
mutex = ctypes.windll.kernel32.CreateMutexW(None, False, "YEYANG_MyProgramMutex")
if ctypes.windll.kernel32.GetLastError() == 183:
show_notification(None, None)
return
# 创建系统托盘图标
image = Image.open("imgs/icon.png")
icon = Icon("椰羊自动化", image, "椰羊自动化")
menu = (
item('冒泡', maopao),
item('退出', exit_program),
)
icon.menu = menu
maopao()
'''
try:
mynd = list_handles(f=lambda n:"notif" in n[-9:])[0]
win32gui.ShowWindow(mynd, 0)
except:
pass
'''
t_notify = threading.Thread(target=notify)
t_notify.start()
# 显示系统托盘图标
icon.run()
if __name__ == '__main__':
main()