pyqt5利用pyqtDesigner實現(xiàn)登錄界面
本文實例為大家分享了pyqt5利用pyqtDesigner實現(xiàn)登錄界面的具體代碼,供大家參考,具體內(nèi)容如下
為便于操作 界面和邏輯分離
邏輯類:
import sys import pymysql from loginUI import * //導入 from PyQt5.QtWidgets import * from PyQt5 import QtWidgets, QtCore, QtGui from PyQt5.QtCore import * from PyQt5.QtGui import class Login(QtWidgets.QDialog): def __init__(self): super(Login, self).__init__() self.ui = Ui_login_Ui() self.ui.setupUi(self) # self.setMinimumSize(QtCore.QSize(400, 200)) # 控制縮放范圍 # self.setMaximumSize(QtCore.QSize(400, 200)) self.setWindowTitle("歡迎使用停車場管理系統(tǒng)") self.setFixedSize(self.width(), self.height()) self.ui.labelTip.hide() self.ui.labelTip.setText("密碼或用戶名不能為空!") # 設(shè)置label字體 labelFont = QFont() labelFont.setPixelSize(15) # 設(shè)置動態(tài)背景 self.gif = QMovie('bg2.gif') self.ui.label_2.setMovie(self.gif) self.gif.start() # 這在label屬性 self.ui.labelTip.setStyleSheet( "QLabel{color:red;font-size:12px;font-weight:bold;font-family:Roman times;}" ) self.ui.userLabel.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(100,100,100,250);font-size:15px;font-weight:bold;font-family:Roman times;}" "QLabel:hover{color:rgb(300,300,300,120);}") self.ui.pwdlabel.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(100,100,100,250);font-size:15px;font-weight:bold;font-family:Roman times;}" "QLabel:hover{color:rgb(300,300,300,120);}") self.ui.label.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(100,100,100,250);font-size:15px;font-weight:bold;font-family:Roman times;}" "QLabel:hover{color:rgb(300,300,300,120);}") self.ui.loginButton.setStyleSheet("QPushButton{color:black}" "QPushButton:hover{color:red}" "QPushButton{background-color:lightblue}" "QPushButton{border:2px}" "QPushButton{border-radius:10px}" "QPushButton{padding:2px 4px}") self.ui.registerButton.setStyleSheet("QPushButton{color:black}" "QPushButton:hover{color:red}" "QPushButton{background-color:lightgreen}" "QPushButton{border:2px}" "QPushButton{border-radius:10px}" "QPushButton{padding:2px 4px}") palette = QPalette() icon = QPixmap('bg2.gif').scaled(800, 600) palette.setBrush(self.backgroundRole(), QBrush(icon)) self.setPalette(palette) self.ui.userLabel.setFont(labelFont) self.ui.pwdlabel.setFont(labelFont) self.ui.label.setFont(labelFont) # 設(shè)置控件尺寸 # self.ui.userlineEdit.setFrame(False) # self.ui.pwdlineEdit.setFrame(False) self.ui.pwdlineEdit.setEchoMode(QLineEdit.Password)# 輸入框設(shè)為密碼模式 self.ui.pwdlineEdit.setClearButtonEnabled(True) self.ui.userlineEdit.setClearButtonEnabled(True) self.ui.userlineEdit.setFixedWidth(190) self.ui.userlineEdit.setFixedHeight(30) self.ui.pwdlineEdit.setFixedWidth(190) self.ui.pwdlineEdit.setFixedHeight(30) self.ui.comboBox.setFixedWidth(100) self.ui.comboBox.setFixedHeight(28) self.ui.loginButton.setFixedSize(75, 28) self.ui.registerButton.setFixedSize(75, 28) self.ui.loginButton.setShortcut('Enter') # shortcut key # 登陸的槽函數(shù)登陸按鈕 最好寫在init的析構(gòu)函數(shù)中,避免鏈接多次產(chǎn)生異常 self.ui.loginButton.clicked.connect(self.slotLogin) self.ui.registerButton.clicked.connect(self.slotRegister) self.ui.pushButton.clicked.connect(self.findPwd) name = self.ui.userlineEdit.text() pwd = self.ui.pwdlineEdit.text() identity = self.ui.comboBox.currentIndex() identity = str(identity) sql = "select * from administrater where username = '" + name + "' and password = '" + pwd + "' and identity= '"+ identity +"' " db = PyMySQLHelper() db.selectALL(sql) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Enter: self.slotLogin() def findPwd(self): # self.accept() self.u = FPwd_ui() self.u.show() # self.ui.exec() def slotLogin(self): # # 獲得登錄輸入 name = self.ui.userlineEdit.text() pwd = self.ui.pwdlineEdit.text() print(name) db = PyMySQLHelper() identity = self.ui.comboBox.currentIndex() # 獲取下標 identity = str(identity) if name != '' and pwd != '': if identity == '0': sql = "select * from administrater where username = '" + name + "' and " \ "password = '" + pwd + "' and identity= '" + identity + "' " print(sql) # cursor.execute(sql) # results = cursor.fetchall() results = db.selectALL(sql) if results: self.ui1 = Finance() # self.ui1.exec() self.ui1.show() self.close() else: OK = QMessageBox.warning(self, ("警告"), ("""賬號或密碼錯誤!""")) # cursor.close() # conn.close() elif identity == '1': sql = "select * from administrater where username = '" + name + "' and " \ "password = '" + pwd + "' and identity= '" + identity + "' " print(sql) # cursor.execute(sql) # results = cursor.fetchall() results = db.selectALL(sql) if results: self.ui2 = InfoManage() self.ui2.show() self.close() else: OK = QMessageBox.warning(self, ("警告"), ("""賬號或密碼錯誤!""")) elif identity == '2': sql = "select * from administrater where username = '" + name + "' and " \ "password = '" + pwd + "' and identity= '" + identity + "' " print(sql) results = db.selectALL(sql) # print(identity) if results: self.uu = SeatManage() self.uu.exec() self.close() else: OK = QMessageBox.warning(self, ("警告"), ("""賬號或密碼錯誤!""")) elif name == 'admin' and pwd == '123' and identity == '3': self.a = Admin() self.a.show() self.close() else: if name == '': OK = QMessageBox.warning(self, ("警告"), ("""請輸入賬號!""")) if pwd == '': OK = QMessageBox.warning(self, ("警告"), ("""請輸入密碼!""")) # cursor.close() # conn.close() def slotRegister(self): self.i = reUi() # self.i的窗口命名不能重復 self.i.exec_() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) my = Login() my.show() sys.exit(app.exec_())
界面類:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_login_Ui(object): def setupUi(self, login_Ui): login_Ui.setObjectName("login_Ui") login_Ui.resize(581, 533) self.gridLayoutWidget = QtWidgets.QWidget(login_Ui) self.gridLayoutWidget.setGeometry(QtCore.QRect(160, 330, 295, 141)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.loginButton = QtWidgets.QPushButton(self.gridLayoutWidget) self.loginButton.setObjectName("loginButton") self.horizontalLayout_2.addWidget(self.loginButton) self.registerButton = QtWidgets.QPushButton(self.gridLayoutWidget) self.registerButton.setObjectName("registerButton") self.horizontalLayout_2.addWidget(self.registerButton) self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout_2.addWidget(self.pushButton) self.gridLayout.addLayout(self.horizontalLayout_2, 4, 1, 1, 1) self.userlineEdit = QtWidgets.QLineEdit(self.gridLayoutWidget) self.userlineEdit.setObjectName("userlineEdit") self.gridLayout.addWidget(self.userlineEdit, 0, 1, 1, 1) self.userLabel = QtWidgets.QLabel(self.gridLayoutWidget) self.userLabel.setObjectName("userLabel") self.gridLayout.addWidget(self.userLabel, 0, 0, 1, 1) self.labelTip = QtWidgets.QLabel(self.gridLayoutWidget) self.labelTip.setObjectName("labelTip") self.gridLayout.addWidget(self.labelTip, 3, 1, 1, 1) self.pwdlineEdit = QtWidgets.QLineEdit(self.gridLayoutWidget) self.pwdlineEdit.setObjectName("pwdlineEdit") self.gridLayout.addWidget(self.pwdlineEdit, 1, 1, 1, 1) self.pwdlabel = QtWidgets.QLabel(self.gridLayoutWidget) self.pwdlabel.setObjectName("pwdlabel") self.gridLayout.addWidget(self.pwdlabel, 1, 0, 1, 1) self.comboBox = QtWidgets.QComboBox(self.gridLayoutWidget) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.gridLayout.addWidget(self.comboBox, 2, 1, 1, 1) self.label = QtWidgets.QLabel(self.gridLayoutWidget) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 2, 0, 1, 1) self.label_2 = QtWidgets.QLabel(login_Ui) self.label_2.setGeometry(QtCore.QRect(-10, 0, 601, 321)) self.label_2.setText("") self.label_2.setObjectName("label_2") self.retranslateUi(login_Ui) QtCore.QMetaObject.connectSlotsByName(login_Ui) def retranslateUi(self, login_Ui): _translate = QtCore.QCoreApplication.translate login_Ui.setWindowTitle(_translate("login_Ui", "Form")) self.loginButton.setText(_translate("login_Ui", "登 陸")) self.registerButton.setText(_translate("login_Ui", "注 冊")) self.pushButton.setText(_translate("login_Ui", "找回密碼")) self.userLabel.setText(_translate("login_Ui", " 賬 戶")) self.labelTip.setText(_translate("login_Ui", "密碼或用戶名錯誤")) self.pwdlabel.setText(_translate("login_Ui", " 密 碼")) self.comboBox.setItemText(0, _translate("login_Ui", "財務(wù)管理員")) self.comboBox.setItemText(1, _translate("login_Ui", "信息管理員")) self.comboBox.setItemText(2, _translate("login_Ui", "停車場管理員")) self.comboBox.setItemText(3, _translate("login_Ui", "超級管理員")) self.label.setText(_translate("login_Ui", " 請選擇"))
具體代碼:pyqt5實現(xiàn)登錄界面
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python操作MySQL數(shù)據(jù)庫的簡單步驟分享
這篇文章主要給大家介紹了關(guān)于Python操作MySQL數(shù)據(jù)庫的簡單步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04python實現(xiàn)的DES加密算法和3DES加密算法實例
這篇文章主要介紹了python實現(xiàn)的DES加密算法和3DES加密算法,以實例形式較為詳細的分析了DES加密算法和3DES加密算法的原理與實現(xiàn)技巧,需要的朋友可以參考下2015-06-06tensorflow:指定gpu 限制使用量百分比,設(shè)置最小使用量的實現(xiàn)
今天小編就為大家分享一篇tensorflow:指定gpu 限制使用量百分比,設(shè)置最小使用量的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02pyppeteer執(zhí)行js繞過webdriver監(jiān)測方法上
這篇文章主要為大家介紹了pyppeteer執(zhí)行js繞過webdriver監(jiān)測方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04Python UnboundLocalError和NameError錯誤根源案例解析
這篇文章主要介紹了Python UnboundLocalError和NameError錯誤根源解析,本文通過案例分析實例代碼相結(jié)合的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10python中使用mysql數(shù)據(jù)庫詳細介紹
這篇文章主要介紹了python中使用mysql數(shù)據(jù)庫詳細介紹,本文起講解了安裝mysql、安裝MySQL-python、mysql 的基本操作、python 操作mysql數(shù)據(jù)庫基礎(chǔ)等內(nèi)容,需要的朋友可以參考下2015-03-03python爬蟲學習筆記--BeautifulSoup4庫的使用詳解
這篇文章主要介紹了Python中使用Beautiful Soup庫的超詳細教程,示例代碼基于Python2.x版本,極力推薦!需要的朋友可以參考下2021-08-08