Python tips can still implement graphical interface without using Gui

PHPz
Release: 2023-04-12 16:43:07
forward
1628 people have browsed it

If there is something that programmers are afraid of, then I think it may be - the needs have changed again!

No, the customer said after the author developed a browser-based Web application: Program It needs to be run in an internal (no) internal (network) environment...

This means that the Python environment cannot be installed!

Who calls us programmers? Why not develop a GUI version? No, it’s not a problem for me...

But after hearing the time given, I couldn’t calm down anymore...

In order not to affect the customer’s evaluation, we can only give one week!

Conception

Although it is not difficult to create a GUI, it does require sorting out the services and the interactive interfaces with users. If not, you will have to write a separate interface for the GUI, which is obviously not enough time.

No, just think of another way...

Otherwise, just package the web application into an executable program and copy it to the machine to run. There are many similar frameworks, such as Nodejs. Electron[1], Pywebview[2] in Python.

Just wrap the original Web program, then just do it!

The artifact appears

The Web program is developed with Flask, so Python needs to be installed Pywebview as packaging tool.

Create a virtual environment [3] or in the original Web project environment, execute:

pip install pywebview
Copy after login

In Windows system, .Net 4.0 or above is required

Try it out:

import webview

window = webview.create_window('Hello!', 'http://http://www.justdopython.com')
webview.start()
Copy after login
  • Reference the webview library
  • Start a window, set the title to Hello!, and specify the page address
  • Start webview

You can see the following effect:

Python tips can still implement graphical interface without using Gui

Try it first

It’s amazing!

Pywebview supports three modes, simple mode and server mode and thread mode.

Simple mode is equivalent to a customized streaming browser. You can browse by specifying an address, as in the example above.

Server mode is equivalent to packaging a Web application, which means it will start a local server and browse in a customized browser.

Thread mode is more advanced, which means you need to manually maintain the thread status to achieve more advanced gameplay.

For the current needs, we choose the server mode, which is to package a local Web application.

Connecting with Flask

The server mode will provide us with an HTTP Server, as long as the web application is deployed on it.

Because it just shows the code of the actual project, here is a simple Flask application:

Regarding Flask Web application development, you can refer to the Flask article written by the author before

Create one app.py file:

from flask import Flask, render_template, jsonify, request

app = Flask(__name__) # 创建一个应用

@app.route('/') 
def index():# 定义根目录处理器
return render_template('index.html')

@app.route('/detail')
def detail():
return render_template('detail.html') 

if __name__ == '__main__':
app.run() # 启动服务
Copy after login

This application is very simple, with only two pages, accessed through / and /detail.

If you run this code, a Flask application will be started and accessed through http://120.0.0.1:5000.

How to install it in Pywebview?

It’s very simple:

import webview
from app import app

if __name__ == '__main__':
window = webview.create_window('Pywebview', app, height=600, width=1000)
webview.start()
Copy after login
  • Introduce webview
  • Introduce the app you just created
  • Create a webview window and pass app as the url parameter
  • Then start the webview

The key here is to use the Flask application as the url parameter. Webview finds that the passed If the parameter entered is the flask application, the service mode will be started.

After running the program, you can see the same effect as in the browser:

Python tips can still implement graphical interface without using Gui

Connecting to Flask

Directory problem

Now you can package this project into an exe.

First you need to install pyinstaller[4]

pip install pyinstaller
Copy after login

Then enter the program directory to execute:

pyinstall -F -w main.py
Copy after login
  • The F parameter means packaging the program into an executable file, without adding This parameter will be packaged into a folder
  • The w parameter indicates that the command line window will not be displayed when executing the packaged executable program. This feature is only available in Windows systems

Soon, a dist folder will be generated in the program directory, and there will be a main.exe executable file in it. This is the packaged result.

Double-click to run and you can see the effect...

Wait, it doesn’t seem to be what you imagined!

Python tips can still implement graphical interface without using Gui

Connect to Flask

What is going on?

According to the prompt, it is because the template file of the page cannot be found.

When we created the Flask app earlier, we used the default template path, which is the templates directory of the directory where the app.py file is located. Why can’t it be found after packaging?

This This is because in Windows, when the executable file is run, it will be decompressed to a specific directory, and our template file is not packaged into the exe file, so the template file cannot be found during runtime.

完美呈现

如何解决这个问题呢?

作为不使用外部数据或文件的程序,只需要将程序本身打包就可以了,但大部分程序都需要外部数据,比如我们的 Flask 应用,就需要用到静态文件等。

那么如何将它们打包进可执行文件呢?

只需要在打包时多加一个参数就可以了:

pyinstaller main.py -F -w --add-data "./templates/*;templates"
Copy after login

-- add-data 参数表示添加额外的数据 -- ./templates/* 表示需要添加当前目录的 templates 目录中的所有文件 -- ;为分隔符,其后的 templates 表示解压是这些数据所在的目录,这个目录名必须和 创建 app 时 template_folder 参数一致 -- 如果需要用到静态文件,需要额外添加,比如 --add-data "./static/*;static"

这样就能将外部数据一起打包进来了。

打包好后,双击执行,就会发现网页得以完美呈现了。

注意:

如果使用了虚拟环境,必须在虚拟环境中单独安装 pyinstaller,而不能用其他环境中已经安装好的,这是为了包装打包是可以链接所以程序引用的模块

因为 pyinstaller 打包时,找不到被引用的模块时并不报错,而打包好的程序可能会无法执行。

总结

经过一番折腾,终于在客户要求的时间之前将工作完成了,特别高兴。

回头一想,多亏用了 Python 作为主要的开发语言,因为 Python 强悍的社区支持没有找不到的解决方法。

这次经历的另一个启示就是,遇到问题,不要着急就做,可以先想一想,是否有更好的方法,特别在使用 Python 的时候。

比心!

参考代码

​https://www.php.cn/link/0c52d419a421fb13bb58357e67b7fb4b​

[1]Electron: https://www.electronjs.org/

[2]Pywebview: https://pywebview.flowrl.com/

[3]虚拟环境: https://mp.weixin.qq.com/s/WflK5pOKhvPg8zrf_W5mfw

[4]pyinstaller: https://pyinstaller.readthedocs.io/en/stable/


The above is the detailed content of Python tips can still implement graphical interface without using Gui. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template