开发环境:
python3.7
pycharm
安装pyqt5及其工具包
pip install PyQt5
pip install PyQt5-tools
遇到问题:
报错:
HTTP error 403 while getting https://mirrors.bfsu.edu.cn/pypi/web/packages/e1/57/2023316578646e1adab903c
aab714708422f83a57f97eb34a5d13510f4e1/PyQt5-5.15.7.tar.gz#sha256=755121a52b3a08cb07275c10ebb96576d36e320e5
72591db16cfdbc558101594 (from https://mirrors.bfsu.edu.cn/pypi/web/simple/pyqt5/) (requires-python:>=3.7)
Could not install requirement pyqt5 from https://mirrors.bfsu.edu.cn/pypi/web/packages/e1/57/20233165786
46e1adab903caab714708422f83a57f97eb34a5d13510f4e1/PyQt5-5.15.7.tar.gz#sha256=755121a52b3a08cb07275c10ebb96
576d36e320e572591db16cfdbc558101594 because of error 403 Client Error: Forbidden for url: https://mirrors.
bfsu.edu.cn/pypi/web/packages/e1/57/2023316578646e1adab903caab714708422f83a57f97eb34a5d13510f4e1/PyQt5-5.1
5.7.tar.gz
Could not install requirement pyqt5 from https://mirrors.bfsu.edu.cn/pypi/web/packages/e1/57/2023316578646
e1adab903caab714708422f83a57f97eb34a5d13510f4e1/PyQt5-5.15.7.tar.gz#sha256=755121a52b3a08cb07275c10ebb9657
6d36e320e572591db16cfdbc558101594 because of HTTP error 403 Client Error: Forbidden for url: https://mirro
rs.bfsu.edu.cn/pypi/web/packages/e1/57/2023316578646e1adab903caab714708422f83a57f97eb34a5d13510f4e1/PyQt5-
5.15.7.tar.gz for URL https://mirrors.bfsu.edu.cn/pypi/web/packages/e1/57/2023316578646e1adab903caab714708
422f83a57f97eb34a5d13510f4e1/PyQt5-5.15.7.tar.gz#sha256=755121a52b3a08cb07275c10ebb96576d36e320e572591db16
cfdbc558101594 (from https://mirrors.bfsu.edu.cn/pypi/web/simple/pyqt5/) (requires-python:>=3.7)
解决:找到目录\venv\Lib\site-packages
下的pip-{version}.dist-info
文件夹并删除,重新安装pip:
python -m pip install --upgrade pip
或使用命令
easy_install -U pip
其他问题可以尝试更换源
清华 https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣 http://pypi.douban.com/simple/
开发一个桌面应用(不使用tool)
# 开发第一个基于PyQt5的桌面应用
import sys
from PyQt5.QtWidgets import QApplication,QWidget
if __name__ == '__main__':
# 创建QApplication类的实例
app = QApplication(sys.argv)
# 创建一个窗口
w = QWidget()
# 设置窗口尺寸 宽度300,高度150
w.resize(400,200)
# 移动窗口
w.move(300,300)
# 设置窗口的标题
w.setWindowTitle('第一个基于PyQt5的桌面应用')
# 显示窗口
w.show()
# 进入程序的主循环,并通过exit函数确保主循环安全结束(该释放资源的一定要释放)
sys.exit(app.exec_())
使用Qt Designer
配置Qt Designer
file → Settings → Tools → External Tools
Program:E:\mygui\venv\Lib\site-packages\qt5_applications\Qt\bin\designer.exe
(根据自己路径修改)
Working directory:$FileDir$(ui文件的路径下) 或 $ProjectFileDir$(工程路径下)
配置PyUIC
Program:..(自己文件对应位置)\Scripts\pyuic5.exe
Arguments:$FileName$ -o $FileNameWithoutExtension$.py
(Arguments是生成文件名的规则)
Working directory与Qt Designer一致
配置Pyrcc
Program:目录下的Scripts文件夹的pyrcc5.exe文件
Arguments:$FileName$ -o $FileNameWithoutExtension$_rc.py
使用QtDesigner
点击QtDesigner跳转到QT界面
拖动组件调整好后保存会出现.ui文件
.ui文件转.py文件
方法一:
方法二:
命令行输入
python -m PyQt5.uic.pyuic demo.ui -o demo.py