当前位置:网站首页>丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
2022-06-29 12:41:00 【Python 技术】

文 | 豆豆
来源:Python 技术「ID: pythonall」

Python 届的 GUI 有很多库,像鼎鼎大名的 Tkinter、PyGUI 等,但问题是他们生成的 GUI 都不够好看,有种上世纪应用程序的风格,完全不像是互联网时代的产品。
今天给大家推荐一个超级棒的工具 Electron,只需要懂一些简单的 html、css 和 js 知识就能写出跨平台的,互联网风格的应用。
大名鼎鼎的 Visual Studio Code 就是使用 Electron 来编写的。
安装
正式开始之前,需要安装 Node 环境,直接从官网下载 Node.js 安装即可。
安装完成之后请使用以下命令来确保自己的 Node 环境是正常的。
node -v
npm -v之后开始初始化项目。
mkdir electron-app && cd electron-app
npm init至此,在 electron-app 文件夹中会生成一个 package.json 的文件,它看起来应该是下面这个样子,每个字段代表什么含义暂时先不用管,继续往下走。
{
"name": "my-electron-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}最后,将 electron 添加到应用的开发依赖中。
npm install --save-dev electron修改 package.json 文件,添加运行应用的命令。
{
"scripts": {
"start": "electron ."
}
}至此,环境我们是搭建完成了。
GUI
上面说了,Electron 是通过 html、css 和 js 来构建应用的,那我们首先来建一个 html 页面。
在根目录下创建 index.html 页面。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>页面写好了,还需要一个启动入口文件来加载我们的页面。
在根目录下新建 index.js 文件。
const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
})
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
})在 js 文件中我们通过创建一个 BrowserWindow 来加载我们指定的页面 index.html。
通过 npm 来启动我们的项目。
npm start不出意外,你应该可以看到如下页面了。

总结
Electron 是一款非常成熟且好用的可跨平台桌面应用程序开发神器,虽说我们今天只实现了简单版本的 Hello World,但在前端的加持下,写出酷炫的紧跟时代潮流的 GUI 那就是分分钟的事。
那这个 GUI 如何和我们的 Python 程序做交互呢,其一通过 http 接口,其二可以直接执行 Python 的脚本,小伙伴们感兴趣的可以跟着官方文档继续深入研究。
更到玩法请参考 https://www.electronjs.org/
PS:公号内回复「Python」即可进入Python 新手学习交流群,一起 100 天计划!
老规矩,兄弟们还记得么,右下角的 “在看” 点一下,如果感觉文章内容不错的话,记得分享朋友圈让更多的人知道!

【神秘礼包获取方式】
识别文末二维码,回复:1024

边栏推荐
- Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
- 揭秘!付费会员制下的那些小心机!
- Shell judges whether the command is executed successfully
- Autonomous and controllable city! Release of the first domestic artiq architecture quantum computing measurement and control system
- C语言模拟实现所有字符函数
- Notimplementederror: numpy() is only available when Eagle execution is enabled
- @Table burst red
- Qitai observation: professional elites must step on the huge pit of entrepreneurship - learning effect pit
- Three best practices help enterprises improve supply chain security
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?大部分人都会答错!
猜你喜欢

力扣:合并两个有序链表

Repoptimizer: it's actually repvgg2

WinDbg common commands

【毕业季·进击的技术er】1076万毕业生,史上最难就业季?卷又卷不过,躺又躺不平,敢问路在何方?

Huawei machine learning service speech recognition function enables applications to paint "sound" and color

System.currentTimeMillis() 和 System.nanoTime() 哪个更快?大部分人都会答错!

存算一体为何是造芯新方向?|对撞派 x 知存科技

matplotlib的imshow函数显示灰度图像要设置vmin和vmax2个参数

Another "provincial capital university", coming!

Openssl证书工具使用手册
随机推荐
MySQL常用语句和命令汇总
Ordinary users use vscode to log in to SSH and edit the root file
weserver發布地圖服務
Deecamp2022 officially opened! Likaifu and zhangyaqin personally teach master courses 𞓜 innovation
leetcode 903. Effective arrangement of Di sequences
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
Weserver Publishing Map Service
mybaits入门(含实例教程和源码)
Rslo: self supervised lidar odometer (real time + high precision, icra2022)
Cvpr2022 | panopticdepth: a unified framework for depth aware panoramic segmentation
服务器上的RTC时间与世界时间不一致解决办法
Cvpr2022 | knowledge distillation through target aware transformer
AOSP ~ initialization language
Prometheus 2.28.0 new features
Cvpr2022 𞓜 loss problem in weakly supervised multi label classification
技术分享| 融合调度中的广播功能设计
商品搜索引擎—推荐系统设计
维修记录导出的excel表格太大怎么办?
Memorized Function
Shell judges whether the command is executed successfully