本文目录:
前言python处理electron部分参考一番今日
前言
前一篇一番实现了在js里调用python程序,这一篇一番试图将electron页面上输入的一些参数传递给python程序。
python处理
代码语言:javascript复制import sys
import zerorpc
class CalcApi(object):
def eval(self, text):
"""based on the input text, return the int result"""
try:
return eval(text)
except Exception as e:
return 0.0
def echo(self, text):
"""echo any text"""
return text
def parse_port():
return 4242
def main():
addr = 'tcp://127.0.0.1:{}'.format(parse_port())
s = zerorpc.Server(CalcApi())
s.bind(addr)
print('start running on {}'.format(addr))
s.run()
if __name__ == '__main__':
main()
electron部分
index.js
保持不变index.ejs
添加如下输入框
<head>
<meta charset="UTF-8">
<title>Hello Calculator!</title>
</head>
<body>
<h1>Hello Calculator!</h1>
<p>Input something like <code>1 1</code>.</p>
<p>This calculator supports <code> -*/^()</code>,
whitespaces, and integers and floating numbers.</p>
<input id="formula" value="1 2.0 * 3.1 / (4 ^ 5.6)"></input>
<div id="result"></div>
</body>
<script>
require('./render.js')
</script>
- 功能模块
render.js
const zerorpc = require("zerorpc")
let client = new zerorpc.Client()
client.connect("tcp://127.0.0.1:4242")
let formula = document.querySelector('#formula')
let result = document.querySelector('#result')
formula.addEventListener('input', () => {
client.invoke("eval", formula.value, (error, res) => {
if(error) {
console.error(error)
} else {
result.textContent = res
}
})
})
formula.dispatchEvent(new Event('input'))
参考
- electron-vue文档
- electron作为python界面开发入门