记录一下使用qt designer和pyqt5做界面的过程
安装PyQt5
pip install PyQt5 -i https://pypi.douban.com/simple
安装QTdesigner
pip install PyQt5-tools -i https://pypi.douban.com/simple
打开QTdesigner
我的位于下面的目录
D:\Anaconda3\Lib\site-packages\pyqt5_tools\Qt\bin\designer.exe
如果报错
需要将
D:\Anaconda3\Lib\site-packages\PyQt5\Qt\plugins\platforms
复制到
D:\Anaconda3\Lib\site-packages\pyqt5_tools\Qt\bin\platforms
就ok了
Ubuntu特殊情况
如果报错
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
Aborted (core dumped)
需要终端加入
export QT_DEBUG_PLUGINS=1
再运行程序进行调试
查看输出的结果。
解决方案
sudo apt install --reinstall libxcb-xinerama0
可能用到的
sudo ln -sf /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/ /usr/bin/
sudo apt-get install qt5-default qttools5-dev-tools
sudo apt-get install python3-pyqt5
sudo apt-get install libx11-dev libxext-dev libxtst-dev
sudo apt-get install xlibmesa-gl-dev libglu1-mesa-dev
sudo ln -sf /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/ /usr/bin/
sudo apt-get install libcap2-bin
设计界面
设计完成后,保存为.ui文件
在终端中输入命令
pyuic5 .\untitled.ui -o none2.py
(pyuic5.bat在conda库中存在)
修改py文件
文件上部加入
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication
import sys
下部加入
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())