打包python文件成exe格式这个过程只能在windows环境下运行
1. 直接在命令行用pip安装 pyinstaller
pip install pyinstaller
2. 下载安装pyinstaller运行时所需要的windows拓展---pywin32
https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/
pywin32-221.win-amd64-py3.6.exe
3.在命令行中切换到要打包的程序所在目录,直接输入下面的指令即可
pyinstaller -F -w demo.py
参数
含义
-F
指定打包后只生成一个exe格式的文件
-D
–onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
-c
–console, –nowindowed 使用控制台,无界面(默认)
-w
–windowed, –noconsole 使用窗口,无控制台
-p
添加搜索路径,让其找到对应的库。
-i
改变生成程序的icon图标
注意事项:
生成文件中,可能包含一个以warn开头的类似warndemo.txt的警告文件
这很明显,
是因为程序无法自动导入很多的库
使用-p参数添加搜索路径
pyinstaller.py -F -p C:\python27; ..\demo.py
#注意当前目录是我在下一级目录里
2.添加icon图标
pyinstaller.py -F -p C:\python27; -i ..\a.ico ..\demo.py
pycharm+PyQt5+python最新开发环境配置
PyQt5安装
pip install PyQt5 -i https://pypi.douban.com/simple
安装常用的Qt工具:
pip install PyQt5-tools -i https://pypi.douban.com/simple
为了让系统可以正常的识别pyqt5-tools的常用命令,还需将其安装路径添加到系统环境变量path中。
C:\Users\Administrator\Anaconda3\Lib\site-packages\ pyqt5-tools
PyQt5测试
在Python中输入:
import PyQt5
不报错即为正确,查看PyQt5所依赖的模块,通过如下命令查看:
help(PyQt5)
配置pycharm
解释器中是可以看到PyQt5和pyqt5-tools的:
设置里面点击external tools,点击“+”,需要添加Qt Designer 和pyuic 两个选项。
Name:Qt Designer
program:Qt Designer的安装路径 C:\Users\weimjg\AppData\Local\Programs\Python\Python36\Lib\site-packages\pyqt5_tools\designer.exe
Arguments:$FileDir$\$FileName$
directory:$FileDir$
Name:PyUIC
program:安装路径 C:\Users\weimjg\AppData\Local\Programs\Python\Python36\Scripts\pyuic5.exe
Arguments:$FileName$ -o $FileNameWithoutExtension$.py
directory: $FileDir$
Name:PyRCC
program:安装路径 C:\Users\weimjg\AppData\Local\Programs\Python\Python36\Scripts\pyrcc5.exe
Arguments:$FileName$ -o $FileNameWithoutExtension$.py
directory: $FileDir$
桌面应用 Tools -> External Tools
Qt Designer 开发UI
untitled.ui
选模板 MainWindow
添加 label pushButton closeButton
保存
ui文件右键用 External Tools -> PyUIC
转成 untitled.py
启动.py文件中
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from untitled import *
class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.onBtnClicked)
self.closeButton.clicked.connect(self.onCloseClicked)
def onBtnClicked(self):
self.label.setText("改了")
self.pushButton.setText("按钮也改了")
# self.close()
def onCloseClicked(self):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())
price = int(self.price_box.toPlainText())
price_box.toPlainText() 是一个内置的可以读取输入框中的值的函数。顺便提一句,虽然你用得多了以后肯定能记住这些函数,因为他们的名字取得很规范,但是你没必要一开始就去记忆所有这些函数。
通过 Google 诸如 “Qt Textbox 读取数据”一类的关键字来找到我所需要的函数。
tax = (self.tax_rate.value())
value() 是读取spinbox 中值的函数。
total_price = price + ((tax / 100) * price)
total_price_string = "The total price with tax is: " + str(total_price)
我们新建了一个 string 变量来储存我们的最终价格。因为最终直接显示在应用上的将会是一个 string 类型的值:
self.results_window.setText(total_price_string)