You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 2.4 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. import sys
  3. import webview
  4. import argparse
  5. from api.api import API
  6. from config.config import Config
  7. from utils.utils import send_notifycation
  8. from pystray import MenuItem as item
  9. import pystray
  10. from PIL import Image
  11. import threading
  12. import time
  13. import requests
  14. from io import BytesIO
  15. # 前端页面目录
  16. if sys.flags.dev_mode:
  17. MAIN_DIR = os.path.join("..", "dist") # 开发环境
  18. DEBUG = True
  19. else:
  20. MAIN_DIR = os.path.join(".", "web") # 生产环境
  21. DEBUG = False
  22. def WebViewApp(port,dev=False):
  23. api = API() # 本地接口
  24. cfg = Config() # 配置文件
  25. def detect():
  26. api.detectThreading()
  27. global timer
  28. timer = threading.Timer(0.25, detect) # 在回调函数中再次启动定时器
  29. timer.start() # 启动定时器
  30. def exitProgram():
  31. icon.stop()
  32. global timer
  33. timer.cancel()
  34. def action():
  35. global timer
  36. timer.cancel()
  37. icon.stop()
  38. # icon.visible = False
  39. if(dev):
  40. window = webview.create_window(cfg.appName, 'http://localhost:'+str(port)+'/',js_api=api)
  41. webview.start(debug=True, http_server=True) # 启动窗口
  42. else:
  43. template = os.path.join(MAIN_DIR, "index.html") # 设置页面,可以指向远程或本地
  44. window = webview.create_window(title=cfg.appName, url=template, js_api=api) # 创建窗口
  45. api.window=window # 本地接口
  46. webview.start(debug=DEBUG, http_server=True) # 启动窗口
  47. timer = threading.Timer(0.25, detect) # 在回调函数中再次启动定时器
  48. timer.start() # 启动定时器
  49. response = requests.get('http://docs.marlene.top/demo/model/logo.png')
  50. response = response.content
  51. BytesIOObj = BytesIO()
  52. BytesIOObj.write(response)
  53. image = Image.open(BytesIOObj)
  54. menu = (item('打开主页面', action), item('退出', exitProgram))
  55. icon = pystray.Icon("name", image, "EMC", menu)
  56. icon.run()
  57. if __name__ == "__main__":
  58. parser = argparse.ArgumentParser()
  59. parser.add_argument(
  60. '--port',
  61. type=str,
  62. default='3333',
  63. help="端口号")
  64. parser.add_argument(
  65. '--dev',
  66. type=str,
  67. default='False',
  68. help="开发模式")
  69. args = parser.parse_args()
  70. WebViewApp(args.port,args.dev=='True')