pycharm+pyqt5+QtDesigner+pyUIC第一个pyqt5程序

安装pyqt5和pyqt5-tools

pip install pyqt5
pip install pyqt5-tools

在tools中添加QtDesigner和PyUIC

QtDesigner在"./Lib/site-packages/pyqt5_tools/Qt/bin"下,具体视python安装目录而定。PyUIC目录为"./Scripts/pyuic5.exe"。

在pycharm中,File->Settings->Tools->External Tools,然后点击左上角加号,在create tool框中输入Name,选择Program,如添加QtDesigner,Name输入QtDesigner,Proogram中选择"./Lib/site-packages/pyqt5_tools/Qt/bin/designer.exe",添加PyUIC时,Name输入pyuic,program选择"./Scripts/pyuic5.exe",然后点OK,然后Apply。pycharm+pyqt5+QtDesigner+pyUIC第一个pyqt5程序_第1张图片

创建项目

在pycharm工具栏Tools->External Tools->QtDesigner进入Qt界面设计界面。

将Push Button控件和Text Edit控件拖入Form中,在工具栏上点击左上到右下那个箭头,然后左键Push Button按住不放到Form中任意位置

pycharm+pyqt5+QtDesigner+pyUIC第一个pyqt5程序_第2张图片

在弹出Configure Connection对话框中,左边选择clicked(),右边点击Edit,弹出对话框中点击Slots下的加号,输入PushButton_click(),点击OK,选中PushButton_click(),点击OKpycharm+pyqt5+QtDesigner+pyUIC第一个pyqt5程序_第3张图片

将ui界面保存为1.ui到项目目录

通过cmd进入项目目录,运行命令pyuic5 -o ui.py 1.ui将.ui文件转化为.py文件,打开ui.py文件,可以看到代码为

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(40, 40, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setGeometry(QtCore.QRect(30, 90, 331, 181))
        self.textEdit.setObjectName("textEdit")

        self.retranslateUi(Form)
        self.pushButton.clicked.connect(Form.PushBotton_click)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

然后新建main.py文件,输入代码

import sys
from PyQt5 import QtWidgets
from ui import Ui_Form

class MyPyQT_Form(QtWidgets.QWidget,Ui_Form):
    def __init__(self):
        super(MyPyQT_Form,self).__init__()
        self.setupUi(self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    my_pyqt_form = MyPyQT_Form()
    my_pyqt_form.show()
    sys.exit(app.exec_())

为了响应事件,在ui.py文件的Ui_Form类后加上

    def PushBotton_click(self):
        self.textEdit.setText("aaaaaaaaaaaaaaaaaa")

运行main.py文件

pycharm+pyqt5+QtDesigner+pyUIC第一个pyqt5程序_第4张图片

你可能感兴趣的:(GUI开发)