学习资源
视频:【python GUI设计 PyQt5从入门到实践-哔哩哔哩】 https://b23.tv/YcqlJcO
其他:链接:https://pan.baidu.com/s/1FzVBWmAOsNY1sllnxhBhsw?pwd=dz6b 提取码:dz6b
第一个PyQt5窗口程序
认识Qt Designer
打开Qt Designer
Qt Designer窗口区域
Qt Designer窗口区域包括:菜单栏、工具栏、工具箱、窗口设计区域、对象查看器、属性编辑器、信号/槽编辑器、动作编辑器、资源浏览器
其他(快捷键)
新建ctrl+N
预览 ctrl+R
使用Qt Designer创建窗口
MainWindow:一个带有菜单栏、工具栏和状态栏的主窗口
步骤如下,具体可参考视频资源~不作过多赘述:
将.ui转换为.py所遇报错及解决过程
报错1- 提示ModuleNotFoundError: No module named PyQt5
心路历程1:在cmd检查确认了PyQt5模块已经下载,但还是报错;又下载pyuic5-tool模块,还是不行~~
心路历程3:在已有项目的最后面找到External Libraries ,然后找到venv目录,把pyvenv.cfg文件中的如下参数改成true~报错完美解决!!
include-system-site-packages = true
报错2- 提示Usage: pyuic5 [options] pyuic5: error: no such option: -m
报错前pyuic配置中的Arguments为:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
又参考的这个博文,我修改了PyUIC配置,稀里糊涂得就解决了~但又出现了报错3
报错3- 提示Error: one input ui-file must be specified
按照这个博文,我把pyuic配置arguments中的空格、.ui文件名称中的空格删掉,报错3解决啦~后又显示报错4呜呜
报错4- 提示Error: No such file or directory
我把.ui文件的路径放在和图中标黄的相同路径下,回到setting>>external tools 界面 ,将 默认的working directory修改为$FileDir$,再转换成.py文件,至此终于成功实现0报错啦!!
运行主程序
当生成.ui文件转换成py文件之后,要在.py文件的最后添加一些代码。
import sys
#程序入口,程序从此处启动PyQt设计的窗口
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow() #创建窗口对象
ui = Ui_MainWindow() #创建PyQt设计的窗口对象
ui.setupUi(MainWindow) #调用PyQt窗体的方法对窗体对象进行初始化设置
MainWindow.show() #显示窗体
sys.exit(app.exec_()) #程序关闭时退出进程
附:PyUIC配置
Program(这里要选择自己pyuic5.exe坐在路径哦)
D:\Tool\python\Scripts\pyuic5.exe
Arguments
$FileName$ -o $FileNameWithoutExtension$.py
Working directory
$FileDir$