Python on Electron框架 [英] Python on Electron framework

查看:426
本文介绍了Python on Electron框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Web技术(HTML5,CSS和JS)编写跨平台的桌面应用程序。我看了一些框架,并决定使用Electron框架。

I am trying to write a cross-platform desktop app using web technologies (HTML5, CSS, and JS). I took a look at some frameworks and decided to use the Electron framework.

我已经用Python完成了该应用程序,所以我想知道是否有可能在Electron框架上使用Python编写跨平台的桌面应用程序?

I've already done the app in Python, so I want to know if is possible to write cross-platform desktop applications using Python on the Electron framework?

推荐答案

可以与Electron一起使用,但是如果您正在寻找 webbish UI功能,则可以检查 Flexx -它允许您使用纯Python进行编码,但仍使用Web开发工具的样式和UI灵活性。

It is possible to work with Electron but if you are looking for "webbish" UI capabilities, you can check Flexx - it allows you to code in pure Python but still use the styling and UI flexibility of web development tools.

如果您坚持要进行Electron,则应遵循发布

If you insist on going on Electron you should follow the idea of this post.

首先请确保已安装所有内容:

First make sure you have everything installed:

pip install Flask
npm install electron-prebuilt -
npm install request-promise -g

现在创建您希望所有魔术发生的目录,并包括以下fil es

Now create the directory where you want all the magic to happen and include following files

创建您的 hello.py

from __future__ import print_function
import time
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World! This is powered by Python backend."

if __name__ == "__main__":
   print('oh hello')
    #time.sleep(5)
    app.run(host='127.0.0.1', port=5000)

创建基本的 package.json

{
  "name"    : "your-app",
  "version" : "0.1.0",
  "main"    : "main.js",
  "dependencies": {
    "request-promise": "*",
    "electron-prebuilt": "*"
  }
}

最后创建您的 main.js

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
electron.crashReporter.start();

var mainWindow = null;

app.on('window-all-closed', function() {
  //if (process.platform != 'darwin') {
    app.quit();
  //}
});

app.on('ready', function() {
  // call python?
  var subpy = require('child_process').spawn('python', ['./hello.py']);
  //var subpy = require('child_process').spawn('./dist/hello.exe');
  var rq = require('request-promise');
  var mainAddr = 'http://localhost:5000';

  var openWindow = function(){
    mainWindow = new BrowserWindow({width: 800, height: 600});
    // mainWindow.loadURL('file://' + __dirname + '/index.html');
    mainWindow.loadURL('http://localhost:5000');
    mainWindow.webContents.openDevTools();
    mainWindow.on('closed', function() {
      mainWindow = null;
      subpy.kill('SIGINT');
    });
  };

  var startUp = function(){
    rq(mainAddr)
      .then(function(htmlString){
        console.log('server started!');
        openWindow();
      })
      .catch(function(err){
        //console.log('waiting for the server start...');
        startUp();
      });
  };

  // fire!
  startUp();
});

来自帖子本身-以下注释

Taken from the post itself - are the following notes


请注意,在main.js中,我们为Python应用程序生成了一个子进程。然后,我们使用无限循环检查服务器是否已启动(嗯,不好的做法!我们应该检查所需的时间,并在几秒钟后中断循环)。服务器启动后,我们将建立一个指向新的本地网站索引页面的实际电子窗口。

Notice that in main.js, we spawn a child process for a Python application. Then we check whether the server has been up or not using unlimited loop (well, bad practice! we should actually check the time required and break the loop after some seconds). After the server has been up, we build an actual electron window pointing to the new local website index page.

这篇关于Python on Electron框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆