PySide2动态/静态加载UI及程序发布

Python目前已经是一个“家喻户晓”的名词了,她可能用在很多行业领域,最牛逼的人工智能(AI)、大数据(big data)。今天要介绍的是Python图形化界面实现(GUI),记得在Python刚出来的时候,开发一个功能强大而美观的GUI还是挺不方便的,最早期也就是Python自己的一个GUI模块库,叫Tkinter,当然现在这个模块功能与比当初要丰富很多了,除此之外,像WxPython、pyGtk、Jython、Pywin32、PyQt、PySide\PySide2等等。这么多的图形开发工具中,个人比较喜欢的是PySide2。下面重点要给大家介绍的是,如何将开发好的程序打包成一个可执行文件发布,可以在别人电脑上使用(这里仅介绍在Windows电脑上使用)。

PySide2介绍

PySide2其实是Python版的Qt,在Qt官方叫"Qt for Python",这样说大家应该就明白了,其实就是Qt的各种图形化库、功能模块库给Python开发调用,在Python中导入一个模块相信大家都知道"import",所以使用PySide2就这么简单。而需要学的,就是"Qt for Python"相关的API函数接口的使用,它按模块分为很多API,比如图形控件类API、网络通讯类API、串口类API、Web相关API等等,我们需要什么API就去看这些API就可以了,就这么easy!

另外,要说明一下PySide2是Qt这家公司的亲儿子,好比kotlin语言是google公司为Android APP开发而准备的一门语言一样(最初Android APP开发只能用java语言开发),现在我们发现很多Android APP都是Kotlin开发,而且功能支持也是Kotlin语言越来越好,毕竟是亲生的,所以PySide2也是这样的。那领养的又是谁呢,就是上面我提到的PyQt,它是一个专门的公司为Qt而开发Python相关API的公司,当然了,它比PySide2要出世早了。

关于版本问题,PySide2是基于Qt5而开发的,而她前面还有一个PySide,是早期的版本,基于Qt4的,也是最早的版本,现在几乎没人用了,原因是功能还不完善。而最新的是PySide6,是不是感觉这跳得有点快,从2一下子跳到6,可能是跟随老子的步伐吧,Qt现在发展到6.0版本,所以直接就PySide6了,但6.0版本由于很多API还没有完全支持,所以本文以PySide2介绍,她几乎与Qt5是同步的,Qt5有的功能她几乎也是能实现的。
(PyQt—>PyQt5—>PyQt6这是领养的版本发展图)

打包神器Pyinstaller

打包python的工具很多,除这个,比如py2exe,cx_freeze也都是不错的工具。

1.安装Pyinstaller打包工具

Microsoft Windows [版本 10.0.19041.867]
(c) 2020 Microsoft Corporation. 保留所有权利。

C:\Users\Gary>pip install -i https://pypi.douban.com/simple pyinstaller
Looking in indexes: https://pypi.douban.com/simple
Collecting pyinstaller
  Downloading https://pypi.doubanio.com/packages/b4/83/9f6ff034650abe9778c9a4f86bcead63f89a62acf02b1b47fc2bfc6bf8dd/pyinstaller-4.2.tar.gz (3.6 MB)
     |████████████████████████████████| 3.6 MB 46 kB/s
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting altgraph
  Downloading https://pypi.doubanio.com/packages/ee/3d/bfca21174b162f6ce674953f1b7a640c1498357fa6184776029557c25399/altgraph-0.17-py2.py3-none-any.whl (21 kB)
Collecting pyinstaller-hooks-contrib>=2020.6
  Downloading https://pypi.doubanio.com/packages/27/c7/58a634d861e4744ac62dca4a4992ace8def8b05dab91e6b25e5043e79acf/pyinstaller_hooks_contrib-2021.1-py2.py3-none-any.whl (181 kB)
     |████████████████████████████████| 181 kB ...
Requirement already satisfied: setuptools in d:\users\python\python39\lib\site-packages (from pyinstaller) (49.2.1)
Collecting pefile>=2017.8.1; sys_platform == "win32"
  Downloading https://pypi.doubanio.com/packages/36/58/acf7f35859d541985f0a6ea3c34baaefbfaee23642cf11e85fe36453ae77/pefile-2019.4.18.tar.gz (62 kB)
     |████████████████████████████████| 62 kB 408 kB/s
Collecting pywin32-ctypes>=0.2.0; sys_platform == "win32"
  Downloading https://pypi.doubanio.com/packages/9e/4b/3ab2720f1fa4b4bc924ef1932b842edf10007e4547ea8157b0b9fc78599a/pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
Collecting future
  Downloading https://pypi.doubanio.com/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829 kB)
     |████████████████████████████████| 829 kB 819 kB/s
Using legacy 'setup.py install' for pefile, since package 'wheel' is not installed.
Using legacy 'setup.py install' for future, since package 'wheel' is not installed.
Building wheels for collected packages: pyinstaller
  Building wheel for pyinstaller (PEP 517) ... done
  Created wheel for pyinstaller: filename=pyinstaller-4.2-py3-none-any.whl size=2413076 sha256=fa9cdc6ec88e2d0c3df74e7aec0f71330feb1b2b6fb751f188bb498631837d06
  Stored in directory: c:\users\gary\appdata\local\pip\cache\wheels\af\ea\26\5812f58861dc385ff5573249b18dfe1624c5f84ea67fa76397
Successfully built pyinstaller
Installing collected packages: altgraph, pyinstaller-hooks-contrib, future, pefile, pywin32-ctypes, pyinstaller
    Running setup.py install for future ... done
    Running setup.py install for pefile ... done
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.2 pyinstalle
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JXES智能生态系统

如文章对你有用,请作者喝个咖啡

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

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

打赏作者

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

抵扣说明:

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

余额充值