164 lines
4.8 KiB
Python
164 lines
4.8 KiB
Python
"""
|
|
质量追溯系统 - Windows 系统托盘
|
|
|
|
双击 tray.py 启动:
|
|
1. 启动后端(隐藏子进程,无控制台窗口)
|
|
2. 系统托盘图标(蓝色)
|
|
3. 双击图标 → 打开浏览器查看前端
|
|
4. 右键图标 → 退出
|
|
|
|
依赖:
|
|
pip install pystray Pillow
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import subprocess
|
|
import threading
|
|
import time
|
|
import webbrowser
|
|
import tkinter as tk
|
|
from tkinter.scrolledtext import ScrolledText
|
|
from pathlib import Path
|
|
|
|
# ── Paths ──────────────────────────────────────────────
|
|
SCRIPT_DIR = Path(__file__).parent.resolve()
|
|
PROJECT_DIR = (SCRIPT_DIR / '..' / 'project').resolve()
|
|
LOG_DIR = PROJECT_DIR / 'logs'
|
|
|
|
# ── Backend process ────────────────────────────────────
|
|
backend = None
|
|
|
|
def start_backend():
|
|
global backend
|
|
env = os.environ.copy()
|
|
env['PORT'] = '3000'
|
|
env['SKIP_PLC'] = '1'
|
|
|
|
if sys.platform == 'win32':
|
|
# Windows: npx.cmd needs shell=True; hide console
|
|
backend = subprocess.Popen(
|
|
'npx tsx src/index.ts',
|
|
cwd=str(PROJECT_DIR),
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
env=env,
|
|
shell=True,
|
|
creationflags=0x08000000,
|
|
)
|
|
else:
|
|
backend = subprocess.Popen(
|
|
['npx', 'tsx', 'src/index.ts'],
|
|
cwd=str(PROJECT_DIR),
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
env=env,
|
|
)
|
|
|
|
# Read stdout/stderr in background for log window
|
|
def read_stream(stream, prefix=''):
|
|
for line in iter(stream.readline, b''):
|
|
text = line.decode('utf-8', errors='replace').rstrip()
|
|
if text:
|
|
append_log(f'{prefix}{text}')
|
|
stream.close()
|
|
|
|
threading.Thread(target=read_stream, args=(backend.stdout, ''), daemon=True).start()
|
|
threading.Thread(target=read_stream, args=(backend.stderr, '[E] '), daemon=True).start()
|
|
|
|
def stop_backend():
|
|
if backend and backend.poll() is None:
|
|
backend.terminate()
|
|
backend.wait(timeout=5)
|
|
|
|
# ── Log window ─────────────────────────────────────────
|
|
log_window = None
|
|
log_text = None
|
|
|
|
def append_log(line):
|
|
if log_text:
|
|
log_text.after(0, lambda: _do_append(line))
|
|
|
|
def _do_append(line):
|
|
log_text.insert(tk.END, line + '\n')
|
|
log_text.see(tk.END)
|
|
|
|
def toggle_log_window():
|
|
global log_window
|
|
if log_window and log_window.winfo_exists():
|
|
log_window.deiconify()
|
|
log_window.lift()
|
|
else:
|
|
create_log_window()
|
|
|
|
def create_log_window():
|
|
global log_window, log_text
|
|
log_window = tk.Toplevel()
|
|
log_window.title('质量追溯系统 - 日志')
|
|
log_window.geometry('850x500')
|
|
log_window.protocol('WM_DELETE_WINDOW', lambda: log_window.withdraw())
|
|
|
|
log_text = ScrolledText(log_window, font=('Consolas', 10), bg='#1e1e2e',
|
|
fg='#cdd6f4', insertbackground='white', state='normal')
|
|
log_text.pack(fill=tk.BOTH, expand=True)
|
|
|
|
# Colour tags
|
|
log_text.tag_configure('i', foreground='#cdd6f4')
|
|
log_text.tag_configure('w', foreground='#f9e2af')
|
|
log_text.tag_configure('e', foreground='#f38ba8')
|
|
log_text.tag_configure('d', foreground='#6c7086')
|
|
|
|
# ── System tray ────────────────────────────────────────
|
|
def create_tray():
|
|
try:
|
|
from PIL import Image, ImageDraw
|
|
except ImportError:
|
|
print('[tray] 请安装依赖: pip install pystray Pillow')
|
|
sys.exit(1)
|
|
|
|
import pystray
|
|
|
|
# Create a simple blue circle icon
|
|
img = Image.new('RGBA', (32, 32), (0, 0, 0, 0))
|
|
draw = ImageDraw.Draw(img)
|
|
draw.ellipse([4, 4, 28, 28], fill=(0x1a, 0x23, 0x7e, 255))
|
|
|
|
def on_show(_):
|
|
webbrowser.open('http://localhost:3000')
|
|
|
|
def on_quit(_):
|
|
stop_backend()
|
|
icon.stop()
|
|
sys.exit(0)
|
|
|
|
menu = (
|
|
pystray.MenuItem('打开页面', on_show, default=True),
|
|
pystray.MenuItem('退出', on_quit),
|
|
)
|
|
|
|
icon = pystray.Icon('scanner-record', img, '质量追溯系统', menu)
|
|
|
|
# Start backend after tray is running
|
|
threading.Thread(target=start_backend, daemon=True).start()
|
|
|
|
icon.run()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# Check dependencies
|
|
try:
|
|
import pystray
|
|
from PIL import Image
|
|
except ImportError:
|
|
print('请先安装依赖: pip install pystray Pillow')
|
|
input('按 Enter 键退出...')
|
|
sys.exit(1)
|
|
|
|
try:
|
|
create_tray()
|
|
except KeyboardInterrupt:
|
|
stop_backend()
|
|
except Exception as e:
|
|
print(f'错误: {e}')
|
|
input('按 Enter 键退出...')
|