gpt4 book ai didi

python - 在 node-webkit 启动时运行 python 脚本 (nw.js)

转载 作者:太空狗 更新时间:2023-10-30 01:21:46 24 4
gpt4 key购买 nike

我有一个基于 Python 的应用程序来控制 LED。它使用 Flask 创建网络服务器,这样我就可以使用 HTML、CSS 和 JS 使用漂亮的 UI。

我目前的流程:

  1. python home.py
  2. 在浏览器中导航到 localhost:5000
  3. 利润!

我想更进一步,将其打包为 nw.js(以前的 node-webkit)应用程序。

基本上,我相信我只需要在窗口加载之前执行我的 python 脚本,这样我久经考验的 Flask Web 服务器就会启动并为我的 localhost:5000 创建一个页面 nw.js 应用界面打开。

如何打包我的 nw.js 应用,使其在启动时运行 python 脚本?

最佳答案

您可以创建加载页面并在网络服务器启动后呈现实际应用。

包.json:

{
"name": "pythonApp",
"version": "0.0.0",
"main": "loading.html",
"window": {
"title": "App Name",
"width": 800,
"height": 600,
"position": "center",
}
}

加载页面 (loading.html) 将加载一个 js 文件,该文件以隐藏窗口的形式启动您的实际应用程序页面,然后您可以在服务器运行时显示它。

loading.html:

var gui = require('nw.gui');

var currentWindow = gui.Window.get(); // Get reference to loading.html

var exec = require('child_process').execFile;
exec('home.py', {cwd:'.'}, function (error, stdout, stderr){ // Runs your python code
var appWindow = gui.Window.open('app.html', // Starts your application
{ width: 800,
height: 600,
position: 'center',
focus: false,
transparent: true // This hides the application window
}
);
appWindow.on('loaded', function() { // Waits for application to be loaded
currentWindow.close({force: 'true'}); // Closes loading.html
appWindow.show(); // Shows app.html
appWindow.focus(); // Set the focus on app.html
});
});

无论如何,这就是它的要点,但您可能需要针对您的特定设置进行更改。希望对您有所帮助。

关于python - 在 node-webkit 启动时运行 python 脚本 (nw.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28964442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com