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中让MySQL查询结果返回字典类型的方法
Aug 22 Python
python中pygame模块用法实例
Oct 09 Python
Django框架中方法的访问和查找
Jul 15 Python
python中redis的安装和使用
Dec 04 Python
Python操作word常见方法示例【win32com与docx模块】
Jul 17 Python
Python解决两个整数相除只得到整数部分的实例
Nov 10 Python
对pandas中时间窗函数rolling的使用详解
Nov 28 Python
pyQT5 实现窗体之间传值的示例
Jun 20 Python
基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)
Aug 06 Python
python中如何实现将数据分成训练集与测试集的方法
Sep 13 Python
python pip如何手动安装二进制包
Sep 30 Python
关于python scrapy中添加cookie踩坑记录
Nov 17 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实现的蚂蚁爬杆路径算法代码
2015/12/03 PHP
PHP常见的6个错误提示及解决方法
2016/07/07 PHP
搜索附近的人PHP实现代码
2018/02/11 PHP
javascript 全等号运算符使用说明
2010/05/31 Javascript
JS 操作符整理[推荐收藏]
2011/11/15 Javascript
Javascript封装DOMContentLoaded事件实例
2014/06/12 Javascript
jquery移动节点实例
2015/01/14 Javascript
jQuery调取jSon数据并展示的方法
2015/01/29 Javascript
JavaScript中windows.open()、windows.close()方法详解
2016/07/28 Javascript
jQuery实现产品对比功能附源码下载
2016/08/09 Javascript
jQuery的事件预绑定
2016/12/05 Javascript
AngularJS表单提交实例详解
2017/02/18 Javascript
node.js(express)中使用Jcrop进行图片剪切上传功能
2017/04/21 Javascript
mongoose设置unique不生效问题的解决及如何移除unique的限制
2017/11/07 Javascript
微信小程序使用Promise简化回调
2018/02/06 Javascript
angularJs 表格添加删除修改查询方法
2018/02/27 Javascript
详解如何探测小程序返回到webview页面
2019/05/14 Javascript
Python编程实现粒子群算法(PSO)详解
2017/11/13 Python
pandas pivot_table() 按日期分多列数据的方法
2018/11/16 Python
Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】
2019/03/18 Python
Python 计算任意两向量之间的夹角方法
2019/07/05 Python
如何用python开发Zeroc Ice应用
2021/01/29 Python
关于HTML5的22个初级技巧(图文教程)
2012/06/21 HTML / CSS
HTML5 video 事件应用示例
2014/09/11 HTML / CSS
日本最大级玩偶手办购物:あみあみ Amiami
2018/04/23 全球购物
Hurley官方网站:扎根于海滩生活方式的全球青年文化品牌
2020/05/18 全球购物
shell的种类有哪些
2015/04/15 面试题
大学生毕业自我评价范文分享
2013/11/07 职场文书
应届优秀本科大学毕业生自我鉴定
2014/01/21 职场文书
幼师自我鉴定
2014/02/01 职场文书
借款协议书范本
2014/04/22 职场文书
劳动竞赛口号
2014/06/16 职场文书
医药销售自我评价200字
2014/09/11 职场文书
2016年先进班集体事迹材料
2016/02/26 职场文书
 Python 中 logging 模块使用详情
2022/03/03 Python
Java字符串逆序方法详情
2022/03/21 Java/Android