Understanding Creating GUI Applications Using Python and Electron

Asked 2 years ago, Updated 2 years ago, 108 views

Environment
ubuntu 16.04 LTS
python 2.712, python 3.5.2
electron1.8.2

I wanted to develop a GUI application using Python, so I used the following page to try to develop using Electron.

https://qiita.com/yoshizaki_kkgk/items/da9711c26e71522ad289

I copied and pasted the code on the above page and installed the module listed on the page, but when I ran the electron command, the terminal did not display any error messages and the electron did not start.
I don't know if main.js itself isn't working well or Python's program itself isn't.
However, the main.js itself may have a problem because the characters are not printed to the console in main.js, but it is hard to think of a solution because it is a complete copy paste.
Electron itself worked to create a simple application using JavaScript only, so it should be fine.

//main.js    
// Electron side initial configuration
const electron=require('electron');
const app = electron.app;
const BrowserWindow=electron.BrowserWindow;
let mainWindow;

// Quit when you close the app
app.on('window-all-closed', function(){
  app.quit();
});

// Post-Application Processing
app.on('ready', function(){
  var subpy=require('child_process').spawn('python', ['./hello.py']);
  varrq = require('request-promise');
  varmainAddr='http://localhost:5000';

  var openWindow=function(){
    mainWindow = new BrowserWindow ({width:400, height:300});
    mainWindow.loadURL(mainAddr);

    // termination processing
    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){
        startUp();
      });
  };

  startUp();
});



# hello.py
#!/usr/bin/env python
# -*-coding:utf-8-*-
from__future_import print_function
import time
from flash import Flash

app = Flask(__name__)

@ app.route("/")

def hello():
    return "Hello World!<br>This is powered by Python backend."

if__name__=="__main__":
    print('on hello')
    app.run (host="127.0.0.1", port=5000)

I changed the commands on the page a little.Below is the description.

$npm install request-g
$ npm install request-promise-g 

package.json

{
  "name": "PythonApp",
  "version": "0.1.0",
  "main" : "main.js",
  "dependencies": {
    "request-promise": "*",
    "electron-prebuild": "*"
  }
}

If anyone knows how to solve this problem, please do so.

javascript python ubuntu electron

2022-09-30 15:32

1 Answers

It may have already been resolved, but just in case.

var subpy=require('child_process').spawn('python', ['../hello.py']);

Is there a python path to the environment variable?
where spawn('python', ['./hello.py']); is

python hello.py

Because it is synonymous with issuing the command, if the path is not configured, this is most likely the cause.


2022-09-30 15:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.