forked from pangao1990/myChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (75 loc) · 2.36 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Author: 潘高
LastEditors: 潘高
Date: 2022-03-23 15:41:46
LastEditTime: 2023-04-07 10:28:03
Description: 生成客户端主程序
usage: 运行前,请确保本机已经搭建Python3开发环境,且已经安装 pywebview 模块。
'''
import argparse
import os
import sys
import webview
from api.api import API
from pyapp.config.config import Config
from pyapp.db.db import DB
cfg = Config() # 配置
db = DB() # 数据库类
api = API() # 本地接口
cfg.init()
def on_shown():
# print('程序启动')
db.init() # 初始化数据库
def on_loaded():
# print('DOM加载完毕')
pass
def on_closing():
# print('程序开始关闭')
pass
# db.close() # 关闭数据库
def on_closed():
# print('程序关闭完成')
pass
def WebViewApp(ifCef=False):
# 是否为开发环境
Config.devEnv = sys.flags.dev_mode
# Config.devEnv = True
# 前端页面目录
if Config.devEnv:
# 开发环境
MAIN_DIR = f'http://localhost:{Config.devPort}/'
template = os.path.join(MAIN_DIR, "") # 设置页面,指向远程
else:
# 生产环境
MAIN_DIR = os.path.join(".", "web")
template = os.path.join(MAIN_DIR, "index.html") # 设置页面,指向本地
# 分辨率
screens = webview.screens
screens = screens[0]
width = screens.width
height = screens.height
initWidth = int(width * 2 / 3)
initHeight = int(height * 4 / 5)
minWidth = int(initWidth / 2)
minHeight = int(initHeight / 2)
# 创建窗口
window = webview.create_window(title=Config.appName, url=template, js_api=api, width=initWidth, height=initHeight, min_size=(minWidth, minHeight))
# 获取窗口实例
api.setWindow(window)
# 绑定事件
window.events.shown += on_shown
window.events.loaded += on_loaded
window.events.closing += on_closing
window.events.closed += on_closed
# CEF模式
guiCEF = 'cef' if ifCef else None
# 启动窗口
webview.start(debug=Config.devEnv, http_server=True, gui=guiCEF)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--cef", action="store_true", dest="if_cef", help="if_cef")
args = parser.parse_args()
ifCef = args.if_cef # 是否开启cef模式
WebViewApp(ifCef)