electron + vuecli + elementplus + python + thrift 制作桌面应用

安装环境

安装npm

官方:http://nodejs.cn/download/
在这里插入图片描述

# npm升级
sudo npm install -g npm

# 安装完成查看nodejs版本
npm -v

安装vuecli

官方:https://cli.vuejs.org/zh/guide/installation.html

# 全局安装vue
sudo npm install -g @vue/cli

# 安装完成查看vue版本
vue --version

创建vue项目

# 打开vueui服务,使用页面进行本机vue管理
vue ui

在这里插入图片描述
在这里插入图片描述
完成创建工作

添加vue-cli-plugin-electron-builder插件

vue-cli-plugin-electron-builder

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

添加Element Plus运行依赖

Element Plus

在这里插入图片描述
在这里插入图片描述

添加thrift插件

thrift

在这里插入图片描述

官方下载thrift

windows

https://thrift.apache.org/download
可以加到path中这样就可以在cmd中直接进行使用

mac

brew install thrift

验证安装成功

thrift -version

你将得到的工程

在这里插入图片描述
在这里插入图片描述

前端开发

设置electron客户端自定义窗口样式

https://www.electronjs.org/zh/docs/latest/tutorial/window-customization

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

添加element中的button,并关联到electron的console.log

全局引用elementplus

https://element-plus.org/zh-CN/guide/quickstart.html

// main.ts
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'

const app = createApp(App)

app.use(ElementPlus)
app.mount('#app')

加入button

https://element-plus.org/zh-CN/component/button.html

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

添加preload.js文件

https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/guide.html#preload-files
在这里插入图片描述

const path = require('path')
preload: path.join(__dirname, 'preload.js')
# vue.config.js
module.exports = {
    pluginOptions: {
        electronBuilder: {
            preload: 'src/preload.js',
        }
    }
}

添加响应事件

hellowolrd.vue
在这里插入图片描述
preload.js
在这里插入图片描述
background.js

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

开启electronserver,查看样式

在这里插入图片描述

在这里插入图片描述

后端开发

安装python的thrift

pip3 install thrift

在这里插入图片描述

整合开发

参考官网编写thrift接口文档

# 生成py代码
thrift -r --gen py tutorial.thrift
# 生成nodejs代码
thrift -r --gen js:node tutorial.thrift

官方关键字说明

https://thrift.apache.org/docs/idl.htmlÏÎÎÎ

官方示例

https://raw.githubusercontent.com/apache/thrift/master/test/ThriftTest.thrift

py示例

https://thrift.apache.org/tutorial/py.html

nodejs示例

https://thrift.apache.org/tutorial/nodejs.html

使用thrift.exe生成python和nodejs的代码

thrift接口定义

electron.thrift
service xiesi
{
    string add(1: i8 arg0, 2: i8 arg1)
}
# 生成py代码
thrift -out ./ -r --gen py electron.thrift
# 生成nodejs代码
thrift -out ./ -r --gen js:node electron.thrift 

python调整部分

py目录结构

在这里插入图片描述

xiesi.py(生成的文件)

代码调整
在这里插入图片描述
main.py

import xiesi
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer


class hello:
    def add(self, arg0: int, arg1: int):
        print("{}_{}".format(arg0, arg1))
        return str(arg0 + arg1)


if __name__ == "__main__":
    handler = hello()
    processor = xiesi.Processor(handler)
    transport = TSocket.TServerSocket(host="127.0.0.1", port=9090)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    print("Starting the server...")
    server.serve()
    print("done.")

electron部分

目录结构

在这里插入图片描述

background.js
const thrift = require("thrift");
var xiesi = require('./xiesi.js');
var transport = thrift.TBufferedTransport;
var protocol2 = thrift.TBinaryProtocol;
var connection = thrift.createConnection("localhost", 9090, {
    transport: transport,
    protocol: protocol2
});

connection.on('error', function (err) {
    console.log(err)
});


function handleSayHello(event, message) {
    console.log("main get:" + message)


    if (connection.connected == false) {
        connection = thrift.createConnection("localhost", 9090, {
            transport: transport,
            protocol: protocol2
        });
    }
    // Create a Calculator client with the connection
    var client = thrift.createClient(xiesi, connection);
    client.add(1, 23, function (err, response) {
        console.log(response)
    });

    return "main say hello"
}

效果图

python

在这里插入图片描述

electron

在这里插入图片描述

python 打包

pyinstaller  main.py -p /Users/xxx/WorkStations/vue/electronpy/electron
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢斯

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值