当前位置:脚本大全 > > 正文

pyqt5 登录界面(pyqt5利用pyqtDesigner实现登录界面)

时间:2021-10-25 10:44:13类别:脚本大全

pyqt5 登录界面

pyqt5利用pyqtDesigner实现登录界面

本文实例为大家分享了pyqt5利用pyqtDesigner实现登录界面的具体代码,供大家参考,具体内容如下

为便于操作 界面和逻辑分离

逻辑类:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 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("欢迎使用停车场管理系统")
  •   self.setFixedSize(self.width(), self.height())
  •   self.ui.labelTip.hide()
  •   self.ui.labelTip.setText("密码或用户名不能为空!")
  •   # 设置label字体
  •   labelFont = QFont()
  •   labelFont.setPixelSize(15)
  •   # 设置动态背景
  •   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)
  •   # 设置控件尺寸
  •   # self.ui.userlineEdit.setFrame(False)
  •   # self.ui.pwdlineEdit.setFrame(False)
  •   self.ui.pwdlineEdit.setEchoMode(QLineEdit.Password)# 输入框设为密码模式
  •   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
  •   # 登陆的槽函数登陆按钮 最好写在init的析构函数中,避免链接多次产生异常
  •   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_())
  • 界面类:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 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标签:

    猜您喜欢