如何用Qt-Designer制作一个登录界面并验证数据库账号进行登录

效果图展示
在这里插入图片描述
登录成功界面
在这里插入图片描述

UI设计代码:
1、将代码文件命名为:dingdanlogin.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dingdanlogin.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(770, 594)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(140, 140, 221, 321))
        self.label.setStyleSheet("border-image: url(:/images/iamges/libai.jpg);\n"
"border-top-left-radius: 30px;\n"
"border-bottom-left-radius: 30px;\n"
"")
        self.label.setText("")
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(360, 140, 251, 321))
        self.label_2.setStyleSheet("border-top-right-radius: 30px;\n"
"border-bottom-right-radius: 30px;\n"
"background-color: qlineargradient(spread:pad, x1:0, y1:0.00568182, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));")
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(410, 180, 171, 41))
        self.label_3.setStyleSheet("font: 16pt \"微软雅黑\";\n"
"color: rgb(150, 150, 150);")
        self.label_3.setObjectName("label_3")
        self.lineEdit_name = QtWidgets.QLineEdit(Form)
        self.lineEdit_name.setGeometry(QtCore.QRect(410, 240, 161, 31))
        self.lineEdit_name.setStyleSheet("border-radius:10px;")
        self.lineEdit_name.setText("")
        self.lineEdit_name.setObjectName("lineEdit_name")
        self.bt_denglu = QtWidgets.QPushButton(Form)
        self.bt_denglu.setGeometry(QtCore.QRect(410, 340, 80, 31))
        self.bt_denglu.setStyleSheet("QPushButton{\n"
"background-color: qlineargradient(spread:reflect, x1:0.534, y1:0, x2:0.523, y2:1, stop:0 rgba(170, 170, 170, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:10px;\n"
"color: rgb(0, 170, 0);\n"
"font: 12pt \"微软雅黑\";}\n"
"QPushButton:hover{\n"
"    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(170, 170, 170, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"\n"
"QPushButton:pressed{padding-top:7px;\n"
"padding-left:7px;}")
        self.bt_denglu.setObjectName("bt_denglu")
        self.lineEdit_psw = QtWidgets.QLineEdit(Form)
        self.lineEdit_psw.setGeometry(QtCore.QRect(410, 290, 161, 31))
        self.lineEdit_psw.setStyleSheet("border-radius:10px;")
        self.lineEdit_psw.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_psw.setObjectName("lineEdit_psw")
        self.bt_tuichu = QtWidgets.QPushButton(Form)
        self.bt_tuichu.setGeometry(QtCore.QRect(570, 140, 21, 21))
        self.bt_tuichu.setStyleSheet("QPushButton{border:none;\n"
"font: 14pt \"微软雅黑\";\n"
"color: rgb(115, 115, 115);}\n"
"QPushButton:pressed{padding-top:4px;\n"
"padding-left:4px;}\n"
"QPushButton:hover{\n"
"    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(170, 170, 170, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"")
        self.bt_tuichu.setObjectName("bt_tuichu")
        self.bt_zuixiaohua = QtWidgets.QPushButton(Form)
        self.bt_zuixiaohua.setGeometry(QtCore.QRect(550, 140, 21, 21))
        self.bt_zuixiaohua.setStyleSheet("QPushButton{border:none;\n"
"font: 14pt \"微软雅黑\";\n"
"color: rgb(115, 115, 115);}\n"
"QPushButton:pressed{padding-top:4px;\n"
"padding-left:4px;}\n"
"QPushButton:hover{\n"
"    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(170, 170, 170, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"")
        self.bt_zuixiaohua.setObjectName("bt_zuixiaohua")
        self.bt_zhuce = QtWidgets.QPushButton(Form)
        self.bt_zhuce.setGeometry(QtCore.QRect(500, 340, 80, 31))
        self.bt_zhuce.setStyleSheet("QPushButton{\n"
"background-color: qlineargradient(spread:reflect, x1:0.534, y1:0, x2:0.523, y2:1, stop:0 rgba(170, 170, 170, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-radius:10px;\n"
"color: rgb(0, 170, 0);\n"
"font: 12pt \"微软雅黑\";}\n"
"QPushButton:hover{\n"
"    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(170, 170, 170, 255), stop:1 rgba(255, 255, 255, 255));\n"
"}\n"
"\n"
"QPushButton:pressed{padding-top:7px;\n"
"padding-left:7px;}")
        self.bt_zhuce.setObjectName("bt_zhuce")
        self.stackedWidget = QtWidgets.QStackedWidget(Form)
        self.stackedWidget.setGeometry(QtCore.QRect(400, 380, 138, 30))
        self.stackedWidget.setObjectName("stackedWidget")
        self.page = QtWidgets.QWidget()
        self.page.setObjectName("page")
        self.stackedWidget.addWidget(self.page)
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.page_2)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label_4 = QtWidgets.QLabel(self.page_2)
        self.label_4.setStyleSheet("color: rgb(255, 0, 0);")
        self.label_4.setObjectName("label_4")
        self.verticalLayout_2.addWidget(self.label_4)
        self.stackedWidget.addWidget(self.page_2)
        self.page_3 = QtWidgets.QWidget()
        self.page_3.setObjectName("page_3")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.page_3)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label_5 = QtWidgets.QLabel(self.page_3)
        self.label_5.setStyleSheet("color: rgb(255, 0, 0);")
        self.label_5.setObjectName("label_5")
        self.verticalLayout.addWidget(self.label_5, 0, QtCore.Qt.AlignHCenter)
        self.stackedWidget.addWidget(self.page_3)
        self.login_version = QtWidgets.QLabel(Form)
        self.login_version.setGeometry(QtCore.QRect(430, 440, 131, 16))
        self.login_version.setStyleSheet("color: rgb(62, 125, 93);")
        self.login_version.setText("")
        self.login_version.setObjectName("login_version")

        self.retranslateUi(Form)
        self.stackedWidget.setCurrentIndex(0)
        self.bt_tuichu.clicked.connect(Form.close) # type: ignore
        self.bt_zuixiaohua.clicked.connect(Form.showMinimized) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Form)
        Form.setTabOrder(self.lineEdit_name, self.lineEdit_psw)
        Form.setTabOrder(self.lineEdit_psw, self.bt_denglu)
        Form.setTabOrder(self.bt_denglu, self.bt_zhuce)
        Form.setTabOrder(self.bt_zhuce, self.bt_zuixiaohua)
        Form.setTabOrder(self.bt_zuixiaohua, self.bt_tuichu)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label_3.setText(_translate("Form", "一朝有悟臻至化境"))
        self.lineEdit_name.setPlaceholderText(_translate("Form", "账号:"))
        self.bt_denglu.setText(_translate("Form", "登录"))
        self.lineEdit_psw.setPlaceholderText(_translate("Form", "密码:"))
        self.bt_tuichu.setText(_translate("Form", "×"))
        self.bt_zuixiaohua.setText(_translate("Form", "-"))
        self.bt_zhuce.setText(_translate("Form", "注册"))
        self.label_4.setText(_translate("Form", "账号或密码不能为空!"))
        self.label_5.setText(_translate("Form", "账号或密码错误!"))
import data_rc

登录界面运行代码。以及数据库连接代码
以下是通过sql server 进行连接 将这部分代码修改为自己数据库即可

     conn = pymssql.connect(host='',
                           port='',
                           user='',
                           password='',
                           database=''
                           ) 
# - * - coding: utf-8 -

from dingdanlogin import *  # 登录窗口
import pymssql
import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QAbstractItemView
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, pyqtSlot, QPoint

user_name = ""  # 保存登录名称
department = ""  # 保存登录部门

# 用于初始登录修改密码

# 登录界面
class LoginWindow(QMainWindow):
    def __init__(self):  # 初始化
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.setWindowIcon(QIcon('icons/logo.ico'))  # logo
        self.setWindowFlags(Qt.FramelessWindowHint)  # 隐藏非边框
        self.setAttribute(Qt.WA_TranslucentBackground)  # 背景透明
        self.ui.bt_denglu.setShortcut('Return')  # 绑定回车键
        self.get_version()                       # 获取版本号
        self.ui.bt_denglu.clicked.connect(self.get_user)  # 登录按钮绑定事件  get_user

    # 获取版本信息
    def get_version(self):
        if os.path.exists('version'):
            os.rename('version', 'version.txt')  # 重命名 version 为 version.txt
            with open('version.txt', 'r') as f:
                a = f.read()
                f.close()
                print('客户端版本:', a)
            print('显示版本信息')
            self.ui.login_version.setText(a)
            os.rename('version.txt', 'version')
        else:
            self.ui.login_version.setText('Version:error')

    # 登录账号密码 数据库验证
    def get_user(self):
        a = 0
        name = self.ui.lineEdit_name.text()
        pwd = self.ui.lineEdit_psw.text()
        name_list = []
        psw_list = []
        department_list = []            # 部门列表存储
        flag_list = []                  # 首次登录修改密码列表

        if name == '' or pwd == '':
            self.ui.stackedWidget.setCurrentIndex(1)
            # QMessageBox.warning(self, '提示', '账号或密码不能为空')
            return
        # 连接数据库 调用db.py文件里面的一个方法,其返回的是一个db值
        try:
            conn = pymssql.connect(host='',
                                   port='',
                                   user='',
                                   password='',
                                   database=''
                                   )
            if conn:
                print('防差错服务器数据库连接成功')
            cursor = conn.cursor()
            select_sql = "select name,password,department,flag from [用户表]"  # 查询语句
            print(select_sql)
            cursor.execute(select_sql)
            self.data = cursor.fetchall()
            cursor.close()
            # conn.close()
            print(self.data)
            # 将账号 和密码  部门 分别存起来。
            for item in self.data:
                # print(item)
                name_list.append(item[0].strip())
                psw_list.append(item[1].strip())
                department_list.append(item[2].strip())
                flag_list.append(item[3])

            print(len(name_list))
            for i in range(len(name_list)):
                if name == name_list[i] and pwd == psw_list[i]:
                    QMessageBox.information(self,'提示','登录成功')

        except Exception as e:
            QMessageBox.information(self, '提示', '请检查是否连接公司本地网络')
            return

        if a == 0:
            pass
        self.ui.stackedWidget.setCurrentIndex(2)

if __name__ == '__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)  # 自适应大小
    app = QApplication(sys.argv)
    win = LoginWindow()
    win.show()
    sys.exit(app.exec())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙崎大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值