如何将一个网站做成桌面应用

1、答案是使用cefpython

#coding=utf-8

from cefpython3 import cefpython as cef
import platform
import sys

#pyinstaller -F -w test3.py --hidden-import inspect --hidden-import json

def main():
    check_versions()
    sys.excepthook = cef.ExceptHook
    
    settings = {
        "debug": False,  # 调试模式
        "log_severity": cef.LOGSEVERITY_INFO, # 日志的输出级别
        "log_file": "debug.log",  # 设置日志文件
        "user_agent": "from tdzx 0.0.1", 
    }
    switches = {
        "enable-media-stream": "",  # 取消获取媒体流(如音频、视频数据),必须以空字符串代表否!
        #"proxy-server": "socks5://127.0.0.1:8888",  # 设置代理
        "disable-gpu": "GPU",  # 设置渲染方式CPU or GPU
    }

    cef.Initialize(settings=settings, switches=switches)
    br = cef.CreateBrowserSync(url="https://www.baidu.com/", window_title="百度", settings={})
    
    #br.ExecuteFunction("alert","hello world!")
    '''
    js = cef.JavascriptBindings()
    js.SetFunction("py_func", py_func)
    js.SetProperty("other", {"a": 1})
    br.SetJavascriptBindings(js)
    '''
    
    cef.MessageLoop()
    cef.Shutdown()

def py_func():
    print("i am in python")

def check_versions():
    ver = cef.GetVersion()
    print("[hello_world.py] CEF Python {ver}".format(ver=ver["version"]))
    print("[hello_world.py] Chromium {ver}".format(ver=ver["chrome_version"]))
    print("[hello_world.py] CEF {ver}".format(ver=ver["cef_version"]))
    print("[hello_world.py] Python {ver} {arch}".format(
           ver=platform.python_version(),
           arch=platform.architecture()[0]))
    assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"


if __name__ == '__main__':
    main()

 

我用官方的示例做了测试,在eclipse中很完美,能跑起来。

 

 

 

不过在编译成可执行文件时却整整弄了两天

编译命令:pyinstaller -F -w test3.py

刚开始使用的环境是python38, 出问题时主要看了这篇帖子:https://stackoverflow.com/questions/64339096/failed-to-create-executable-with-pyinstaller-and-cefpython-on-linux-invalid-fil

有所感悟,但始终解决不了问题。好处是我感觉是python版本的问题,所以我就装了第二个版本python35

 

 

然后各种改环境变量,先把pip这些都指向py35,一通操作后编译成功,不过跑起来会有各种错,先参考了https://stackoverflow.com/questions/52572708/cefpython-cx-freeze-errors

几经折腾发现

 

 

这些不都是我要的吗?我就给复制到dist目录了

 

 

辛苦没有白费,跑起来了,非常nice

 

posted @ 2023-02-17 15:54  河北大学-徐小波  阅读(100)  评论(0编辑  收藏  举报