Python3+PyQt5+PyCharm 桌面GUI開發環境搭建

Python3+PyQt5+PyCharm 桌面GUI開發環境搭建

1、安裝python

PyQt5所支持的python版本是不低於3.5版本python

python3.5以上的版本安裝:https://www.python.org/downloads/windows/web

2、安裝PyQt5

pip install PyQt5
pip install PyQt5-tools

3、配置pycharm

官網下載安裝pycharm:https://www.jetbrains.com/pycharm/windows

1,點擊:File -》Settingsapp

2,Tools -》 External Tools  -》點擊「+」號ui

 

3,設置Qt Designerthis

修改三個地方,其餘地方默認:spa

Name:Qt Designer
Programs:D:\Program Files\Python35\Lib\site-packages\pyqt5-tools\designer.exe
Working directory:$ProjectFileDir$

注意:Programs參數須要修改成你電腦裏邊的「designer.exe」路徑。code

4,配置PyUICorm

設置四個地方,其餘能夠默認
Name:PyUIC
Programs:D:\Program Files\Python35\python.exe
Parameters:-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py
Working directory:$ProjectFileDir$

注意:Programs參數須要修改成你電腦裏邊的python「python.exe」路徑。blog

4、使用Qt Designer

1,完成以上步驟以後,點擊 Tools -》External Tools -》Qt Designer 啓動咱們的Qt Designer。

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_())
相關文章
相關標籤/搜索