pyqt5利用pyqtDesigner实现登录界面


Posted in Python onMarch 28, 2019

本文实例为大家分享了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 相关文章推荐
Python的Django中django-userena组件的简单使用教程
May 30 Python
Python使用turtule画五角星的方法
Jul 09 Python
在Python的Django框架中编写编译函数
Jul 20 Python
python使用matplotlib绘制柱状图教程
Feb 08 Python
开源软件包和环境管理系统Anaconda的安装使用
Sep 04 Python
mac下如何将python2.7改为python3
Jul 13 Python
详解python while 函数及while和for的区别
Sep 07 Python
django中账号密码验证登陆功能的实现方法
Jul 15 Python
Pycharm使用远程linux服务器conda/python环境在本地运行的方法(图解))
Dec 09 Python
Python基础之高级变量类型实例详解
Jan 03 Python
python网络编程socket实现服务端、客户端操作详解
Mar 24 Python
Python3读写ini配置文件的示例
Nov 06 Python
Django实现单用户登录的方法示例
Mar 28 #Python
pyqt5实现登录界面的模板
May 30 #Python
python实现AES加密和解密
Mar 27 #Python
详解Python计算机视觉 图像扭曲(仿射扭曲)
Mar 27 #Python
python实现向微信用户发送每日一句 python实现微信聊天机器人
Mar 27 #Python
Pandas读写CSV文件的方法示例
Mar 27 #Python
使用Python的SymPy库解决数学运算问题的方法
Mar 27 #Python
You might like
使用 MySQL 开始 PHP 会话
2006/12/21 PHP
ThinkPHP的MVC开发机制实例解析
2014/08/23 PHP
PHP给源代码加密的几种方法汇总(推荐)
2018/02/06 PHP
javascript instanceof 与typeof使用说明
2010/01/11 Javascript
{}与function(){}选用空对象{}来存放keyValue
2012/05/23 Javascript
jquery的选择器的使用技巧之如何选择input框
2013/09/22 Javascript
使用javascript实现简单的选项卡切换
2015/01/09 Javascript
javascript获得当前的信息的一些常用命令
2015/02/25 Javascript
jQuery 操作input中radio的技巧
2016/07/18 Javascript
JS中对数组元素进行增删改移的方法总结
2016/12/15 Javascript
elementUi vue el-radio 监听选中变化的实例代码
2019/06/28 Javascript
vue遍历生成的输入框 绑定及修改值示例
2019/10/30 Javascript
JavaScript多种滤镜算法实现代码实例
2019/12/10 Javascript
python和shell实现的校验IP地址合法性脚本分享
2014/10/23 Python
Python3.2中Print函数用法实例详解
2015/05/19 Python
使用pip发布Python程序的方法步骤
2018/10/11 Python
python制作简单五子棋游戏
2019/06/18 Python
50行Python代码实现视频中物体颜色识别和跟踪(必须以红色为例)
2019/11/20 Python
浅析Python 责任链设计模式
2020/09/11 Python
Html5移动端弹幕动画实现示例代码
2018/08/27 HTML / CSS
Canvas获取视频第一帧缩略图的实现
2020/11/11 HTML / CSS
手工制作的豪华英式沙发和沙发床:Willow & Hall
2019/05/03 全球购物
介绍一下grep命令的使用
2015/06/12 面试题
恒华伟业笔试面试题
2015/02/26 面试题
高校毕业生自我鉴定
2013/10/27 职场文书
一封普通求职者的求职信
2013/11/20 职场文书
公司新员工的演讲稿注意事项
2014/01/01 职场文书
买房子个人收入证明
2014/01/16 职场文书
校园联欢晚会主持词
2014/03/17 职场文书
英语故事演讲稿
2014/04/29 职场文书
学校学雷锋活动总结
2014/06/26 职场文书
2014年医生工作总结
2014/11/21 职场文书
爱心捐款感谢信
2015/01/20 职场文书
实习报告范文之电话客服岗位
2019/07/26 职场文书
Java移除无效括号的方法实现
2021/08/07 Java/Android
Python使用pandas导入xlsx格式的excel文件内容操作代码
2022/12/24 Python