《快速掌握PyQt5》第二十五章 Pyinstaller打包

第二十五章 Pyinstaller打包

25.1 下载Pyinstaller

25.2 了解Pyinstaller命令参数

25.3 打包示例

25.4 小结


写完的程序如果要发给别人使用,但对方并没有安装python环境,也没有安装PyQt5库时怎么办呢?最好的解决办法就是将程序打包成可执行文件,这样就算在一台没有安装python环境和PyQt5库的电脑上也可以使用,非常方便。

 

25.1 下载Pyinstaller

windows上下载:

pip install pyinstaller

 

Linux上下载:

pip3 install pyinstaller

 

Mac上下载:

pip3 install pyinstaller

 

下载完后我们打开终端,输入pyinstaller,若显示如下,则表示安装成功:

 

25.2 了解Pyinstaller命令参数

这里我们只需要了解几个常用命令即可,详细用法请参考Pyinstaller手册:

参数用处
-F将程序打包成一个文件
-w去除黑框
-i添加程序图标

 

25.3 打包示例

我们就将第五章的登录框小程序拿过来打包作为示例好了,该程序的代码如下:

import sys

from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QLabel, QLineEdit, QPushButton, \

QGridLayout, QVBoxLayout, QHBoxLayout, QMessageBox

USER_PWD = {

'la_vie': 'password'

}

class Demo(QWidget):

def __init__(self):

super(Demo, self).__init__()

self.resize(300, 100)

self.user_label = QLabel('Username:', self)

self.pwd_label = QLabel('Password:', self)

self.user_line = QLineEdit(self)

self.pwd_line = QLineEdit(self)

self.login_button = QPushButton('Log in', self)

self.signin_button = QPushButton('Sign in', self)

self.grid_layout = QGridLayout()

self.h_layout = QHBoxLayout()

self.v_layout = QVBoxLayout()

self.lineedit_init()

self.pushbutton_init()

self.layout_init()

self.signin_page = SigninPage() # 实例化SigninPage()

def layout_init(self):

self.grid_layout.addWidget(self.user_label, 0, 0, 1, 1)

self.grid_layout.addWidget(self.user_line, 0, 1, 1, 1)

self.grid_layout.addWidget(self.pwd_label, 1, 0, 1, 1)

self.grid_layout.addWidget(self.pwd_line, 1, 1, 1, 1)

self.h_layout.addWidget(self.login_button)

self.h_layout.addWidget(self.signin_button)

self.v_layout.addLayout(self.grid_layout)

self.v_layout.addLayout(self.h_layout)

self.setLayout(self.v_layout)

def lineedit_init(self):

self.user_line.setPlaceholderText('Please enter your username')

self.pwd_line.setPlaceholderText('Please enter your password')

self.pwd_line.setEchoMode(QLineEdit.Password)

self.user_line.textChanged.connect(self.check_input_func)

self.pwd_line.textChanged.connect(self.check_input_func)

def pushbutton_init(self):

self.login_button.setEnabled(False)

self.login_button.clicked.connect(self.check_login_func)

self.signin_button.clicked.connect(self.show_signin_page_func)

def check_login_func(self):

if USER_PWD.get(self.user_line.text()) == self.pwd_line.text():

QMessageBox.information(self, 'Information', 'Log in Successfully!')

else:

QMessageBox.critical(self, 'Wrong', 'Wrong Username or Password!')

self.user_line.clear()

self.pwd_line.clear()

def show_signin_page_func(self):

self.signin_page.exec_()

def check_input_func(self):

if self.user_line.text() and self.pwd_line.text():

self.login_button.setEnabled(True)

else:

self.login_button.setEnabled(False)

class SigninPage(QDialog):

def __init__(self):

super(SigninPage, self).__init__()

self.signin_user_label = QLabel('Username:')

self.signin_pwd_label = QLabel('Password:')

self.signin_pwd2_label = QLabel('Password:')

self.signin_user_line = QLineEdit()

self.signin_pwd_line = QLineEdit()

self.signin_pwd2_line = QLineEdit()

self.signin_button = QPushButton('Sign in')

self.user_h_layout = QHBoxLayout()

self.pwd_h_layout = QHBoxLayout()

self.pwd2_h_layout = QHBoxLayout()

self.all_v_layout = QVBoxLayout()

self.lineedit_init()

self.pushbutton_init()

self.layout_init()

def layout_init(self):

self.user_h_layout.addWidget(self.signin_user_label)

self.user_h_layout.addWidget(self.signin_user_line)

self.pwd_h_layout.addWidget(self.signin_pwd_label)

self.pwd_h_layout.addWidget(self.signin_pwd_line)

self.pwd2_h_layout.addWidget(self.signin_pwd2_label)

self.pwd2_h_layout.addWidget(self.signin_pwd2_line)

self.all_v_layout.addLayout(self.user_h_layout)

self.all_v_layout.addLayout(self.pwd_h_layout)

self.all_v_layout.addLayout(self.pwd2_h_layout)

self.all_v_layout.addWidget(self.signin_button)

self.setLayout(self.all_v_layout)

def lineedit_init(self):

self.signin_pwd_line.setEchoMode(QLineEdit.Password)

self.signin_pwd2_line.setEchoMode(QLineEdit.Password)

self.signin_user_line.textChanged.connect(self.check_input_func)

self.signin_pwd_line.textChanged.connect(self.check_input_func)

self.signin_pwd2_line.textChanged.connect(self.check_input_func)

def pushbutton_init(self):

self.signin_button.setEnabled(False)

self.signin_button.clicked.connect(self.check_signin_func)

def check_input_func(self):

if self.signin_user_line.text() and self.signin_pwd_line.text() and self.signin_pwd2_line.text():

self.signin_button.setEnabled(True)

else:

self.signin_button.setEnabled(False)

def check_signin_func(self):

if self.signin_pwd_line.text() != self.signin_pwd2_line.text():

QMessageBox.critical(self, 'Wrong', 'Two Passwords Typed Are Not Same!')

elif self.signin_user_line.text() not in USER_PWD:

USER_PWD[self.signin_user_line.text()] = self.signin_pwd_line.text()

QMessageBox.information(self, 'Information', 'Register Successfully')

self.close()

else:

QMessageBox.critical(self, 'Wrong', 'This Username Has Been Registered!')

self.signin_user_line.clear()

self.signin_pwd_line.clear()

self.signin_pwd2_line.clear()

if __name__ == '__main__':

app = QApplication(sys.argv)

demo = Demo()

demo.show()

sys.exit(app.exec_())

我们将这个py文件命名为test.py,并将文件放到桌面上,之后打开命令行窗口,cd到桌面目录,输入pyinstaller -F -w test.py

点击回车,开始打包:

结束后我们会发现在桌面上多了两个文件夹和一个spec文件:

而我们想要的可执行程序就在dist文件夹中,双击打开就是我们的程序了:

 

现在你完全可以就将这个test可执行文件放到其他电脑上去运行。

 

上面打包的可执行文件使用的是默认的图标,我们接下来打包时添加-i参数来给程序加个图标(请注意该用法只对Windows系统有效):

图标下载:https://www.easyicon.net/download/ico/1210123/64/

将该图标命名为login.ico并放在桌面,跟test.py同路径。同样打开命令行窗口,cd到桌面,输入pyinstaller -F -w -i login.ico test.py 按回车开始打包:

然后在dist文件夹中就可以找到我们打包好的可执行文件:

 

25.4 小结

1. 打包时可能会出现各种各样的问题,但大部分问题其实只要仔细阅读报错内容就可以解决,不要慌;

2. 笔者汇总了一些用Pyinstaller打包时出现的问题,详情请见:Pyinstaller打包问题之解决方案汇总。

 

----------------------------------------------------------------------

喜欢的小伙伴可以加入这个Python QQ交流群一起学习:820934083

以上是 《快速掌握PyQt5》第二十五章 Pyinstaller打包 的全部内容, 来源链接: utcz.com/a/52551.html

回到顶部