electron调用python_Electron as GUI of Python

最近准备做一个离线升级工具,想起前几天刚接触的Electron 决定用它与python相结合来完成

开始准备环境搭建

几经折腾各种zerorpc,zerormq各种报错

历经一天多的网上折腾终于找到一篇实践成功

先看整个流程搭建:

start

|

V

+------------+

| | start

| +-------------> +-------------------+

| electron | sub process | |

| | | python web server |

| (basically | http | |

| browser) | <-----------> | (business logic) |

| | communication | |

| | | (all html/css/js) |

| | | |

+------------+ +-------------------+

start

|

V

+--------------------+

| | start

| electron +-------------> +------------------+

| | sub process | |

| (browser) | | python server |

| | | |

| (all html/css/js) | | (business logic) |

| | zerorpc | |

| (node.js runtime, | <-----------> | (zeromq server) |

| zeromq client) | communication | |

| | | |

+--------------------+ +------------------+

下来安装步骤(windows):

Remove-Item "$($env:USERPROFILE)\.node-gyp" -Force -Recurse -ErrorAction Ignore

Remove-Item "$($env:USERPROFILE)\.electron-gyp" -Force -Recurse -ErrorAction Ignore

Remove-Item .\node_modules -Force -Recurse -ErrorAction Ignore

npm install --runtime=electron --target=1.7.6(版本很重要)

这里我直接远程连接的同事的python环境,修改下面文件:

//main.js

let mainWindow = null

const createWindow = () => {

mainWindow = new BrowserWindow({width: 800, height: 600})

mainWindow.loadURL(require('url').format({

pathname: path.join(__dirname, 'index.html'),

protocol: 'file:',

slashes: true

}))

mainWindow.webContents.openDevTools()

mainWindow.on('closed', () => {

mainWindow = null

})

}

app.on('ready', createWindow)

app.on('window-all-closed', () => {

if (process.platform !== 'darwin') {

app.quit()

}

})

const zerorpc = require("zerorpc")

let client = new zerorpc.Client()

client.connect("tcp://192.168.200.51:4242")

client.invoke("echo", "server ready", (error, res) => {

if(error || res !== 'server ready') {

console.error(error)

} else {

console.log(res)

}

})

let formula = document.querySelector('#formula')

let result = document.querySelector('#result')

formula.addEventListener('input', () => {

client.invoke("calc", formula.value, (error, res) => {

if(error) {

console.error(error)

} else {

result.textContent = res

}

})

})

formula.dispatchEvent(new Event('input'))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值