7 Commits

Author SHA1 Message Date
yshtcn
d7ae897441 Merge pull request #2 from yshtcn/Dev
Dev to main 2023.9.7.2
2023-09-07 20:01:08 +08:00
yshtcn
f2b7cff99d - 修改了范式配置文件 2023-09-07 19:55:08 +08:00
yshtcn
30d055c6b0 - 增加了打开程序目录的功能
- 增加了启动和修改自启动设置时显示通知
2023-09-07 19:54:49 +08:00
yshtcn
17e23ef208 增加第三方库列表,并在打包前自动安装 2023-09-07 19:52:46 +08:00
yshtcn
f6ce8b28bc - 为自启动模块增加系统通知 2023-09-07 17:26:49 +08:00
yshtcn
93b34526e3 自动安装pyinstaller后清空 2023-09-07 11:15:10 +08:00
yshtcn
e5246f5b5c 状态栏图标增加自启动选项 2023-09-07 11:14:30 +08:00
4 changed files with 110 additions and 4 deletions

View File

@@ -18,5 +18,5 @@ proxy_url = socks5://username:password@localhost:1080
# 任务栏托盘标题 # 任务栏托盘标题
title = 系统心跳 title = 系统心跳
# 托盘悬停提示 # 托盘悬停提示
tips = 系统状态报送(首次运行时记得先更新配置) tips = 程序将持续的向设定的目标(首次运行时记得先更新配置)

View File

@@ -1,6 +1,7 @@
import sys import sys
import configparser import configparser
import pystray import pystray
from pystray import MenuItem as item
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
import requests import requests
import time import time
@@ -11,6 +12,29 @@ import os
import shutil import shutil
import subprocess import subprocess
import platform import platform
import winreg
from plyer import notification
def display_notification(title, message, app_name='YourAppName', timeout=10):
"""
Display a Windows notification.
Parameters:
- title (str): The title of the notification.
- message (str): The message content of the notification.
- app_name (str): The name of the application. Default is 'YourAppName'.
- timeout (int): The time (in seconds) before the notification disappears. Default is 10 seconds.
"""
notification.notify(
title=title,
message=message,
app_name=app_name,
timeout=timeout
)
# 获取当前脚本所在的目录 # 获取当前脚本所在的目录
current_dir = os.path.dirname(os.path.realpath(__file__)) current_dir = os.path.dirname(os.path.realpath(__file__))
@@ -141,7 +165,79 @@ heartbeat_ping = config.get('Settings', 'heartbeat_ping', fallback=None)
title = config.get('Settings', 'title') title = config.get('Settings', 'title')
tips = config.get('Settings', 'tips') tips = config.get('Settings', 'tips')
icon = pystray.Icon(title, image, tips, menu=pystray.Menu(pystray.MenuItem('关闭程序', quit_action)))
# 添加程序到Windows启动
def add_to_startup(program_name, executable_path):
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_SET_VALUE)
winreg.SetValueEx(key, program_name, 0, winreg.REG_SZ, executable_path)
winreg.CloseKey(key)
logger.info(f"Program added to startup: Program name = {program_name}, Executable path = {executable_path}")
display_notification(f"{program_name}已添加到自启动项", f"启动路径: {executable_path}")
except Exception as e:
logger.error(f"An error occurred while adding the program to startup: {e}")
display_notification(f"{program_name}未能添加到自启动项目", f"错误原因: {e}")
# 从Windows启动中移除程序
def remove_from_startup(program_name):
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_SET_VALUE)
winreg.DeleteValue(key, program_name)
winreg.CloseKey(key)
logger.info(f"Program removed from startup: Program name = {program_name}")
display_notification(f"{program_name}已从自启动项移除", f"{program_name}将不再自动启动")
except Exception as e:
logger.error(f"An error occurred while removing the program from startup: {e}")
display_notification(f"{program_name}未能从自启动项移除", f"错误原因: {e}")
# 检查程序是否已在Windows启动中
def is_in_startup(program_name):
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, winreg.KEY_READ)
value, _ = winreg.QueryValueEx(key, program_name)
winreg.CloseKey(key)
return True
except WindowsError:
return False
# 检查是否是通过PyInstaller打包的程序
def is_packaged():
return getattr(sys, 'frozen', False)
# 切换Windows启动状态
def toggle_startup(icon, item):
program_name = title
if is_in_startup(program_name):
remove_from_startup(program_name)
else:
add_to_startup(program_name, sys.executable)
def open_current_dir(text):
os.startfile(current_dir)
# 创建状态栏图标和菜单
menu_items = [
item('打开程序目录', open_current_dir),
item('关闭程序', quit_action)
]
# 判断是否以.exe方式运行添加相应菜单项
if is_packaged():
menu_items.insert(0, item('添加/取消自启动', toggle_startup, checked=lambda text: is_in_startup(title)))
else:
menu_items.insert(0, item('Py运行不支持自启动', lambda text: None, enabled=False))
menu = pystray.Menu(*menu_items)
# 创建并运行状态栏图标
icon = pystray.Icon(title, image, tips, menu)
#icon = pystray.Icon(title, image, tips, menu=pystray.Menu(pystray.MenuItem('关闭程序', quit_action)))
# 创建一个新的Session对象并根据需要配置代理 # 创建一个新的Session对象并根据需要配置代理
session = requests.Session() session = requests.Session()
@@ -150,6 +246,7 @@ if config.get('Settings', 'proxy_enabled', fallback='0') == '1':
'https': config.get('Settings', 'proxy_url')} 'https': config.get('Settings', 'proxy_url')}
# Start the heartbeat function in a new thread # Start the heartbeat function in a new thread
display_notification(f'{title}已启动', f'{tips}')
t = threading.Thread(target=heartbeat, args=(interval, heartbeat_url, session, heartbeat_ping)) t = threading.Thread(target=heartbeat, args=(interval, heartbeat_url, session, heartbeat_ping))
t.start() t.start()

View File

@@ -1,8 +1,13 @@
@echo off @echo off
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>pyinstaller" title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>pyinstaller"
:: <20><>װ/<2F><><EFBFBD><EFBFBD>pyinstaller(ע<><EFBFBD><E2A3BA>ϣ<EFBFBD><CFA3><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ/<2F><><EFBFBD><EFBFBD>pyinstaller<65>ǵ<EFBFBD>ע<EFBFBD>͵<EFBFBD><CDB5><EFBFBD> :: <20><>װ/<2F><><EFBFBD><EFBFBD>pyinstaller(ע<><EFBFBD><E2A3BA>ϣ<EFBFBD><CFA3><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>װ/<2F><><EFBFBD><EFBFBD>pyinstaller<65>ǵ<EFBFBD>ע<EFBFBD>͵<EFBFBD><CDB5><EFBFBD>
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>Ŀ<EFBFBD>"
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
cls
:: <20><><EFBFBD>±<EFBFBD><C2B1><EFBFBD> :: <20><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>" title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>"
@@ -100,7 +105,7 @@ rd /S /Q %~dp0\build\build
:: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD> :: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
cd /d %~dp0\build cd /d %~dp0\build
pyinstaller --onefile --noconsole --version-file %~dp0\version_info.txt --add-data "%~dp0\config.Exsample.ini;." %~dp0\heartbeat.py pyinstaller --hidden-import=plyer.platforms.win.notification --onefile --noconsole --version-file %~dp0\version_info.txt --add-data "%~dp0\config.Exsample.ini;." %~dp0\heartbeat.py
::<3A><><EFBFBD>±<EFBFBD><C2B1><EFBFBD> ::<3A><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" title "AutoPyInstaller<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
pystray
Pillow
requests
plyer