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和C/C++交互的几种方法总结
May 11 Python
Python实现自定义函数的5种常见形式分析
Jun 16 Python
django框架实现一次性上传多个文件功能示例【批量上传】
Jun 19 Python
python字典排序的方法
Oct 12 Python
基于Python实现签到脚本过程解析
Oct 25 Python
Tensorflow 卷积的梯度反向传播过程
Feb 10 Python
Django权限设置及验证方式
May 13 Python
Python中内建模块collections如何使用
May 27 Python
Numpy 多维数据数组的实现
Jun 18 Python
只用50行Python代码爬取网络美女高清图片
Jun 02 Python
PyMongo 查询数据的实现
Jun 28 Python
python3操作redis实现List列表实例
Aug 04 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
把PHP安装为Apache DSO
2006/10/09 PHP
php 移除数组重复元素的一点说明
2008/11/27 PHP
10个实用的PHP代码片段
2011/09/02 PHP
php常用Output和ptions/Info函数集介绍
2013/06/19 PHP
教你在header中隐藏php的版本信息
2016/08/10 PHP
基于PHP实现发微博动态代码实例
2020/12/11 PHP
jQuery选择器的工作原理和优化分析
2011/07/25 Javascript
js实现无需数据库的县级以上联动行政区域下拉控件
2013/08/14 Javascript
使用jquery局部刷新(jquery.load)从数据库取出数据
2014/01/22 Javascript
解决Jquery鼠标经过不停滑动的问题
2014/03/03 Javascript
ztree获取选中节点时不能进入可视区域出现BUG如何解决
2015/12/03 Javascript
浅谈JavaScript 执行环境、作用域及垃圾回收
2016/05/31 Javascript
easyui中combotree循环获取父节点至根节点并输出路径实现方法
2016/11/10 Javascript
浅谈jQuery before和insertBefore的区别
2016/12/04 Javascript
JavaScript基本语法_动力节点Java学院整理
2017/06/26 Javascript
微信小程序实现点击按钮修改view标签背景颜色功能示例【附demo源码下载】
2017/12/06 Javascript
React Router v4 入坑指南(小结)
2018/04/08 Javascript
详解微信小程序入门从这里出发(登录注册、开发工具、文件及结构介绍)
2020/07/21 Javascript
Nuxt的路由配置和参数传递方式
2020/11/06 Javascript
python实现的文件同步服务器实例
2015/06/02 Python
深入浅析Python字符编码
2015/11/12 Python
对python中return和print的一些理解
2017/08/18 Python
解决Pycharm下面出现No R interpreter defined的问题
2018/10/29 Python
python opencv读mp4视频的实例
2018/12/07 Python
彻底理解Python中的yield关键字
2019/04/01 Python
CSS3制作Dropdown下拉菜单的方法
2015/07/18 HTML / CSS
Expedia丹麦:全球领先的旅游网站
2018/03/18 全球购物
香港演唱会订票网站:StubHub香港
2019/10/10 全球购物
一套Delphi的笔试题二
2013/05/11 面试题
普师专业个人自荐信范文
2013/11/26 职场文书
园林毕业生自我鉴定范文
2013/12/29 职场文书
人事任命书范文
2014/06/04 职场文书
质监局领导班子对照检查材料思想汇报
2014/09/27 职场文书
成人成长感言如何写?
2019/08/16 职场文书
Django展示可视化图表的多种方式
2021/04/08 Python
Python序列化与反序列化相关知识总结
2021/06/08 Python