PyCharm+PyQt5+QtDesigner 开发桌面工具
一.工具安装
在python下做可视化界面的设计,用QtDesigner来画界面很是方便,当时画完之后都要手动在终端输入pyuic5 -o 1.py 1.ui,将ui转成py,来把.ui文件转换为.py的代码再进行编辑,不是很方便,期望在PyCharm下完成所有工作
配置前提:anaconda、pycharm
1、首先打开anaconda,environment—>not installed搜索pyqt,会看到pyqt的包,直接勾选后右下角apply就安装好了。安完后可以切换到installed查看是否真正安装成功。
2、安装完PyQt5之后,可以在anaconda—>Libra—>bin目录下看到designer.exe,这个就是用来画界面的QtDesigner,记住这个目录,一会儿会用到。
3、打开PyCharm,打开File—>Settings—>External Tools,点击加号来添加自己的工具,做如下配置:
Name:QtDesigner
Group:Qt
Programs:F:\anaconda\Library\bin\designer.exe(这里是各位自己的designer路径,之前所看到的)
Working directory:$ProjectFileDir$
完成后点“OK”,现在QtDesigner就配置好了,就可以直接在PyCharm中打开它来画界面了,但是要在PyCharm中把界面的.ui文件转换为.py文件还需要后面的配置。
4、同样在External Tools中点击加号来配置pyuic:
Name:Pyuic
Group:Qt
Program:F:\anaconda\python.exe(各位自己的python路径)
Arguments:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory:$FileDir$
5、至此就全部配置好了,接下来是test,回到PyCharm,Tools—>Qt—>QtDesigner,点击即可打开designer:
然后创建一个最简单的界面,把这个界面保存(默认是保存在当前pycharm项目目录下,我这里命名“first.ui”)。
6、回到pycharm,可以看到工程目录下已经产生了first.ui,右键它,Qt—>Pyuic,点击后即可产生first.py文件,OK接下来就可以愉快地写代码了
然后就可以不断的修改first.ui文件,每次修改完再转换为.py文件就好了。
二、使用
1.在first.py里添加
if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() # ui_from是类名 ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
2.给按钮添加事件
self.retranslateUi(MainWindow)
self.pushButton_2.clicked.connect(MainWindow.close)
self.pushButton.clicked.connect(self.weather)
self.pushButton_3.clicked.connect(self.textEdit.clear)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
3.界面
4.完整代码
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'first.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets import sys class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(50, 40, 121, 20)) self.label.setObjectName("label") self.toolButton = QtWidgets.QToolButton(Dialog) self.toolButton.setGeometry(QtCore.QRect(50, 240, 61, 21)) self.toolButton.setObjectName("toolButton") self.toolButton_2 = QtWidgets.QToolButton(Dialog) self.toolButton_2.setGeometry(QtCore.QRect(170, 240, 61, 21)) self.toolButton_2.setObjectName("toolButton_2") self.toolButton_3 = QtWidgets.QToolButton(Dialog) self.toolButton_3.setGeometry(QtCore.QRect(280, 240, 61, 21)) self.toolButton_3.setObjectName("toolButton_3") self.lineEdit = QtWidgets.QLineEdit(Dialog) self.lineEdit.setGeometry(QtCore.QRect(170, 40, 161, 20)) self.lineEdit.setObjectName("lineEdit") self.textEdit = QtWidgets.QTextEdit(Dialog) self.textEdit.setGeometry(QtCore.QRect(50, 80, 291, 141)) self.textEdit.setObjectName("textEdit") self.retranslateUi(Dialog) self.toolButton_3.clicked.connect(Dialog.close) self.toolButton.clicked.connect(self.weather) self.toolButton_2.clicked.connect(self.textEdit.clear) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "请输入要查询的城市:")) self.toolButton.setText(_translate("Dialog", ".查询..")) self.toolButton_2.setText(_translate("Dialog", "清除")) self.toolButton_3.setText(_translate("Dialog", "退出")) def weather(self): area = self.lineEdit.text() if area == '': print("提示;请输入要查询的城市") else: info = '{}:下大雨!!!'.format(area) self.textEdit.setText(info) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) #QMainWindow窗口也带有最大化最小化和退出,另外窗口最下面有statusbar可以显示设置的消息 MainWindow = QtWidgets.QMainWindow() ui = Ui_Dialog() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
博客参考
https://blog.csdn.net/qq_36961626/article/details/107792487
https://blog.csdn.net/esting_tang/article/details/81082504