PyQt+PyCharm

4864 ワード

この文書では、Windowsシステムの下でPython+PyCharm+PyQt 5をインストールし、PyQt 5によってGUIインタフェースを2つの方法で設計する方法について説明します.a.コード設計インタフェースを直接使用する.b.QtDesignを使用してビジュアル化し、生成した.uiファイルを.pyファイルに変換します.
pythonのインストール(本人3.7)
PyQtのインストール
pip install PyQt5
pip install PyQt5-tools

インストールpyCharm(自分で解読可能)
配置:1,クリック:File-》Settings 2,Tools-》External Tools-"+"3,Qt Designを設定して3つの場所を修正し、その他の場所はデフォルト:
Name:Qt Designer
Programs:C:\Users\cd\Anaconda3\Lib\site-packages\pyqt5_tools\designer.exe
Working directory:$ProjectFileDir$

注意:Programsパラメータは、あなたのパソコンの「designer.exe」パスに変更する必要があります.
4、PyUIC配置
4つの場所を設定し、その他はデフォルトで設定できます.
Name:PyUIC
Programs:C:\Users\cd\Anaconda3\python.exe
Parameters:-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py
Working directory:$ProjectFileDir$

注意:Programsパラメータは、コンピュータ内のpython「python.exe」パスに変更する必要があります.
四、Qt Design 1を使用して、以上の手順を完了したら、Tools-』External Tools-』Qt Designをクリックして、私たちのQt Designを起動します.
2,起動後選択:Widget,空白のウィンドウを作成し,Createをクリックし,その他のデフォルトでよい
3、左の1区からドラッグして、注意は“ドラッグ”コントロールから作業区まで、対応する属性を修正します
4、基本的なインタフェースの設定が終わると、同じディレクトリの下に「.ui」というファイルが生成されます.
5,External Tools-』PyUICを右クリックし、「.ui」ファイルを「.py」ファイルに変換
6,この場合、すべて正常で、エラーが報告されていなければ、同じディレクトリの下で対応する「.py」ファイルが生成されます.
7、次のコードを生成した「.py」ファイルに入れ、最後に置けばいい(インデント注意)
if __name__=="__main__":
    import sys
    from PyQt5.QtGui import QIcon
    app=QtWidgets.QApplication(sys.argv)
    widget=QtWidgets.QWidget()
    ui=Ui_Form()
    ui.setupUi(widget)
    widget.setWindowIcon(QIcon('web.png'))#  icon  ,      

なくてもいい
widget.show()
sys.exit(app.exec_())

8、運転開始、pythonGUIツアー開始
9、ソースコード:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(552, 288)
        self.username = QtWidgets.QLabel(Form)
        self.username.setGeometry(QtCore.QRect(90, 90, 48, 20))
        self.username.setObjectName("username")
        self.username_2 = QtWidgets.QLabel(Form)
        self.username_2.setGeometry(QtCore.QRect(90, 130, 48, 20))
        self.username_2.setObjectName("username_2")
        self.layoutWidget = QtWidgets.QWidget(Form)
        self.layoutWidget.setGeometry(QtCore.QRect(140, 130, 189, 22))
        self.layoutWidget.setObjectName("layoutWidget")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.layoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.horizontalLayout_2.addWidget(self.lineEdit_2)
        self.radioButton = QtWidgets.QRadioButton(Form)
        self.radioButton.setGeometry(QtCore.QRect(150, 180, 131, 16))
        self.radioButton.setObjectName("radioButton")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(180, 220, 75, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(140, 90, 189, 22))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.lineEdit = QtWidgets.QLineEdit(self.widget)
        self.lineEdit.setObjectName("lineEdit")
        self.horizontalLayout.addWidget(self.lineEdit)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.username.setText(_translate("Form", "   :"))
        self.username_2.setText(_translate("Form", "  :"))
        self.radioButton.setText(_translate("Form", "        "))
        self.pushButton_2.setText(_translate("Form", "  "))

if __name__ == "__main__":
    import sys
    from PyQt5.QtGui import QIcon
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(widget)
    widget.setWindowIcon(QIcon('web.png'))  #   icon  ,            
    widget.show()
    sys.exit(app.exec_())