1.pyqt5工具安裝
(1)使用pip工具安裝PyQt5工具。執行pip install PyQt5
(2)安裝Qt Designer圖形界面開發工具。執行pip install PyQt5-tools
安裝完成後所在路徑
C:\Users\用戶\AppData\Local\Programs\Python\Python38\Lib\site-packages
(3)環境變量配置,將PyQt5-tools的安裝目錄下qt/bin所在目錄添加到系統環境變量path,就可以使用cmd啓動designer
如果出現啓動失敗,將pyqt5_tools裏面qt/plugins/platforms目錄粘貼至與qt/bin/desinger.exe文件同級就解決了
(4)IDE配置,我目前用vscode,安裝插件PYQT Integration
首選項–>設置–>搜索pyqt,最後一項填寫designer路徑
系統會自動配置pyuic,該工具是用於將Qt Designer工具開發完成的.ui文件轉化爲.py文件。
(5)pycharm配置
file–>setting–>tools–>external tools添加
①qt designer
Name:可自己定義
program:Qt Designer的安裝路徑
parameter:不填
directory: $FileDir$
②PyUIC
Name:可自己定義
program:pyuic的安裝路徑
parameter:$FileName$ -o $FileNameWithoutExtension$.py
directory: $FileDir $
2.簡單的login例子
①創建新的ui
②選擇textlabel,pushbutton,textbrowser構成如圖所示
③雙擊可修改對應名稱
④同時將右側對應的from屬性修改
⑤保存成爲login.ui文件,右鍵此文件,選擇compile生成對應的py文件
login.py文件代碼如下,
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.14.2
#
# 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(542, 311)
Form.setMaximumSize(QtCore.QSize(699, 499))
self.user_lable = QtWidgets.QLabel(Form)
self.user_lable.setGeometry(QtCore.QRect(40, 60, 55, 16))
self.user_lable.setObjectName("user_lable")
self.pwd_lable = QtWidgets.QLabel(Form)
self.pwd_lable.setGeometry(QtCore.QRect(40, 100, 55, 16))
self.pwd_lable.setObjectName("pwd_lable")
self.user_lineEdit = QtWidgets.QLineEdit(Form)
self.user_lineEdit.setGeometry(QtCore.QRect(130, 60, 113, 22))
self.user_lineEdit.setObjectName("user_lineEdit")
self.pwd_lineEdit = QtWidgets.QLineEdit(Form)
self.pwd_lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 22))
self.pwd_lineEdit.setObjectName("pwd_lineEdit")
self.login_Button = QtWidgets.QPushButton(Form)
self.login_Button.setGeometry(QtCore.QRect(40, 180, 93, 28))
self.login_Button.setObjectName("login_Button")
self.cancel_Button = QtWidgets.QPushButton(Form)
self.cancel_Button.setGeometry(QtCore.QRect(170, 180, 93, 28))
self.cancel_Button.setObjectName("cancel_Button")
self.user_textBrowser = QtWidgets.QTextBrowser(Form)
self.user_textBrowser.setGeometry(QtCore.QRect(290, 61, 231, 131))
self.user_textBrowser.setObjectName("user_textBrowser")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.user_lable.setText(_translate("Form", "用戶名"))
self.pwd_lable.setText(_translate("Form", "密碼"))
self.login_Button.setText(_translate("Form", "登錄"))
self.cancel_Button.setText(_translate("Form", "退出"))
⑥邏輯實現代碼,同目錄下創建call_login.py文件
import sys
#PyQt5中使用的基本控件都在PyQt5.QtWidgets模塊中
from PyQt5.QtWidgets import QApplication, QMainWindow
#導入designer工具生成的login模塊
from login import Ui_Form
class MyMainForm(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
#添加登錄按鈕信號和槽。注意display函數不加小括號()
self.login_Button.clicked.connect(self.display)
#添加退出按鈕信號和槽。調用close函數
self.cancel_Button.clicked.connect(self.close)
def display(self):
#利用line Edit控件對象text()函數獲取界面輸入
username = self.user_lineEdit.text()
password = self.pwd_lineEdit.text()
#利用text Browser控件對象setText()函數設置界面顯示
self.user_textBrowser.setText("登錄成功!\n" + "用戶名是: "+ username+ ",密碼是: "+ password)
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication對象。sys.argv是命令行參數列表,確保程序可以雙擊運行
app = QApplication(sys.argv)
#初始化
myWin = MyMainForm()
#將窗口控件顯示在屏幕上
myWin.show()
#程序運行,sys.exit方法確保程序完整退出。
sys.exit(app.exec_())