根据你提供的代码,我将进行一些修改以解决可能的问题。以下是修改后的代码:
import webview
import threading
import time
from pystray import Icon, Menu, MenuItem
from PIL import Image
import pygetwindow as gw
import multiprocessing
from printer_monitor import start_monitoring_all_printers
import win32api
import win32con
import win32gui
import os
import webbrowser
url = 'https://www.baidu.com'
def exit_program():
os._exit(0)
def on_click(icon, item):
window.show()
def create_tray_icon():
image = Image.open('./favicon.ico')
menu = Menu(
MenuItem('打印计费系统', on_click),
MenuItem('后台登录', open_url)
)
icon = Icon("test", image, "打印计费系统", menu)
icon.run()
def open_url(icon, item):
webbrowser.open('https://www.baidu.com')
def hide_window_after_delay(window):
while True:
time.sleep(1)
result = window.evaluate_js('localStorage.getItem("isLogin")')
if result == 'true':
window.hide()
break
def move_to_bottom_right(window_title="打印计费系统"):
max_attempts = 10
attempt = 0
hwnd = None
while attempt < max_attempts:
window = gw.getWindowsWithTitle(window_title)
if window:
hwnd = window[0]._hWnd
if hwnd:
break
attempt += 1
time.sleep(1)
if hwnd:
screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
rect = win32gui.GetWindowRect(hwnd)
window_width = rect[2] - rect[0]
window_height = rect[3] - rect[1]
new_x = screen_width - window_width
new_y = screen_height - window_height
win32gui.MoveWindow(hwnd, new_x, new_y, window_width, window_height, True)
else:
print("Failed to get window handle.")
def process_print_jobs(queue, window):
while True:
job = queue.get()
if job is None:
break
processed_job = {
"job_id": job['Job ID'],
"document_name": job['Document'],
"paper_type": "普通纸",
"size": f"{job['Paper Size']}",
"color_mode": job['Color'],
"type": "单面" if job['Duplex'] == 1 else "双面",
"total_pages": job['Pages'],
"copies": job['Copies'],
}
window.evaluate_js(f'addLocalItem({processed_job})')
window.show()
move_to_bottom_right()
if __name__ == "__main__":
if not is_already_running():
job_queue = multiprocessing.Queue()
monitor_process = multiprocessing.Process(target=start_monitoring_all_printers, args=(job_queue,))
monitor_process.start()
window = webview.create_window('打印计费系统', url, width=680, height=450, confirm_close=True, resizable=False, on_top=True)
window.events.closed += exit_program
t1 = threading.Thread(target=hide_window_after_delay, args=(window,))
t1.start()
t2 = threading.Thread(target=process_print_jobs, args=(job_queue, window))
t2.start()
t3 = threading.Thread(target=create_tray_icon)
t3.start()
webview.start(localization={'global.quitConfirmation': u'确定关闭?'})
def is_already_running():
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', 54321))
if result == 0:
return True
else:
sock.bind(('127.0.0.1', 54321))
return False
这里做了以下修改:
- 添加了
is_already_running
函数,在 if __name__ == "__main__":
块开始时调用,以防止程序多次运行。
- 修改了
hide_window_after_delay
函数,使其在检测到登录后退出循环,避免无限递归调用。
- 将
create_tray_icon
函数中的 MenuItem
的 on_click
方法直接设置为 window.show()
,避免不必要的回调。
在重新打包程序之前,请确保已经安装了所有必要的依赖项,并且正确设置了 PyInstaller 的 .spec
文件。如果问题仍然存在,可能需要进一步调试或检查 PyInstaller 的日志输出以获取更多信息。