当前位置:网站首页>丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
2022-06-28 09:46:00 【AI科技大本营】

作者 | 派森酱
来源 | Python技术
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 的脚本,小伙伴们感兴趣的可以跟着官方文档继续深入研究。

往期回顾
分享
点收藏
点点赞
点在看边栏推荐
- Sword finger offer | Fibonacci sequence
- Key summary VII of PMP examination - monitoring process group (1)
- Differences between task parameter types inout and ref
- 大纲笔记软件 Workflowy 综合评测:优点、缺点和评价
- Dbeaver installation and use tutorial (super detailed installation and use tutorial)
- mysql打不开,闪退
- 2020-10-27
- 4 methods for exception handling
- 2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
- Dotnet uses crossgen2 to readytorun DLL to improve startup performance
猜你喜欢

Redis sentinel cluster main database failure data recovery ideas # yyds dry goods inventory #

理想中的接口自动化项目

Proxy mode (proxy)

Decorator

Matplotlib attribute and annotation

函数的分文件编写

Key summary V of PMP examination - execution process group

接口自动化框架脚手架-参数化工具的实现

解决表单action属性传参时值为null的问题

Caffeine cache, the king of cache, has stronger performance than guava
随机推荐
Numpy array: join, flatten, and add dimensions
Function sub file writing
Crawler small operation
R语言使用car包中的avPlots函数创建变量添加图(Added-variable plots)、在图像交互中,在变量添加图中手动标识(添加)对于每一个预测变量影响较大的强影响点
Instant messaging and BS architecture simulation of TCP practical cases
Huawei OSPF single region
Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app
Data visualization makes correlation analysis easier to use
Inventory of excellent note taking software: good-looking and powerful visual note taking software, knowledge map tools heptabase, hydrogen map, walling, reflect, infranodus, tiddlywiki
如图 用sql行转列 图一原表,图二希望转换后
Application of X6 in data stack index management
On the influence of small program on the digitalization of media industry
Wechat applet development log
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)
ECS MySQL query is slow
Summary of MySQL basic knowledge points
云服务器MYSQL查询速度慢
What is the difference between MySQL development environment and test environment??
Sword finger offer | linked list transpose