pyqt5利用pyqtDesigner实现登录界面

 更新时间:2019年03月28日 08:43:19   作者:窥月  
这篇文章主要为大家详细介绍了pyqt5利用pyqtDesigner实现登录界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

逻辑类:

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_())

界面类:

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", "财务管理员"))
  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实现登录界面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 浅谈Python之Django

    浅谈Python之Django

    这篇文章主要介绍了Python3中的Django,小编觉得这篇文章写的还不错,需要的朋友们下面随着小编来一起学习学习吧,希望能够给你带来帮助
    2021-10-10
  • Python实现导弹自动追踪代码实例

    Python实现导弹自动追踪代码实例

    这篇文章主要介绍了Python实现导弹自动追踪代码实例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-11-11
  • Python中的复制操作及copy模块中的浅拷贝与深拷贝方法

    Python中的复制操作及copy模块中的浅拷贝与深拷贝方法

    浅拷贝和深拷贝是Python基础学习中必须辨析的知识点,这里我们将为大家解析Python中的复制操作及copy模块中的浅拷贝与深拷贝方法:
    2016-07-07
  • 详解pandas中MultiIndex和对象实际索引不一致问题

    详解pandas中MultiIndex和对象实际索引不一致问题

    这篇文章主要介绍了详解pandas中MultiIndex和对象实际索引不一致问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • python taipy库轻松地将数据和机器学习模型转为功能性Web应用

    python taipy库轻松地将数据和机器学习模型转为功能性Web应用

    taipy 是一个开源的 Python 库,任何具有基本 Python 技能的人都可以使用,对于数据科学家、机器学习工程师和 Python 程序员来说,它是一个方便的工具,借助 Taipy,你可以轻松地将数据和机器学习模型转变为功能性的 Web 应用程序
    2024-01-01
  • 彻底卸载anaconda详细教程(推荐!)

    彻底卸载anaconda详细教程(推荐!)

    用anaconda更改我的python版本,就是出现了冻结无法更改等等之类的问题,担心更新anaconda还是会出错,于是打算卸载anaconda,重新下载一个安装,下面这篇文章主要给大家介绍了关于彻底卸载anaconda的相关资料,需要的朋友可以参考下
    2022-11-11
  • Python-openCV读RGB通道图实例

    Python-openCV读RGB通道图实例

    今天小编就为大家分享一篇Python-openCV读RGB通道图实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python实战之外星人入侵游戏示例代码

    Python实战之外星人入侵游戏示例代码

    这篇文章主要介绍了利用Python编写的外星人入侵游戏的示例代码,文中的代码讲解详细,对我们学习Python有一定的帮助,感兴趣的可以学习一下
    2022-01-01
  • python登陆asp网站页面的实现代码

    python登陆asp网站页面的实现代码

    这篇文章主要介绍了python登陆asp网站页面的实现代码,需要的朋友可以参考下
    2015-01-01
  • python在回调函数中获取返回值的方法

    python在回调函数中获取返回值的方法

    今天小编就为大家分享一篇python在回调函数中获取返回值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02

最新评论