欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pyqt5利用pyqtDesigner实现登录界面

程序员文章站 2022-06-30 10:46:03
本文实例为大家分享了pyqt5利用pyqtdesigner实现登录界面的具体代码,供大家参考,具体内容如下 为便于操作 界面和逻辑分离 逻辑类: import...

本文实例为大家分享了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", " 请选择"))

具体代码:

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