效果图




代码

本例只要注册,暂时没有登录。

# -*- coding: utf-8 -*-
'''**************************************************************************
版本:1.0
内容:注册界面设计,分文件
时间:2021.9.24
作者:狄云
***************************************************************************'''import pymysql
from PyQt5.QtCore import Qt, pyqtSignal, QPoint
from PyQt5.QtGui import QFont, QEnterEvent, QPainter, QColor, QPen
from PyQt5.QtWidgets import QHBoxLayout, QLabel,QSpacerItem, QSizePolicy
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QTextEdit
from PyQt5 import QtGui
import sysfrom PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QTextEdit
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtGui
from PyQt5.QtGui import QFontStyleSheet = """
/*最小化最大化关闭按钮通用默认背景*/
#buttonMinimum,#buttonMaximum,#buttonClose {border: none;
}
#buttonClose,#buttonMaximum,#buttonMinimum{color:grey;
}
/*悬停*/
#buttonMinimum:hover,#buttonMaximum:hover {color: white;
}
#buttonClose:hover {color: white;
}
/*鼠标按下不放*/
#buttonMinimum:pressed,#buttonMaximum:pressed {color:grey;
}
#buttonClose:pressed {color: white;}
"""
class TitleBar(QWidget):# 窗口最小化信号windowMinimumed = pyqtSignal()# 窗口最大化信号windowMaximumed = pyqtSignal()# 窗口还原信号windowNormaled = pyqtSignal()# 窗口关闭信号windowClosed = pyqtSignal()# 窗口移动windowMoved = pyqtSignal(QPoint)def __init__(self, *args, **kwargs):super(TitleBar, self).__init__(*args, **kwargs)self.setStyleSheet(StyleSheet)self.mPos = Noneself.iconSize = 20  # 图标的默认大小# 布局layout = QHBoxLayout(self, spacing=0)layout.setContentsMargins(0, 0, 0, 0)# 窗口图标self.iconLabel = QLabel(self)
#         self.iconLabel.setScaledContents(True)layout.addWidget(self.iconLabel)# 窗口标题self.titleLabel = QLabel(self)self.titleLabel.setStyleSheet("color:grey")self.titleLabel.setMargin(2)layout.addWidget(self.titleLabel)# 中间伸缩条layout.addSpacerItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))# 利用Webdings字体来显示图标font = self.font() or QFont()font.setFamily('Webdings')# 最小化按钮self.buttonMinimum = QPushButton('0', self, clicked=self.windowMinimumed.emit, font=font, objectName='buttonMinimum')layout.addWidget(self.buttonMinimum)# 最大化/还原按钮self.buttonMaximum = QPushButton('1', self, clicked=self.showMaximized, font=font, objectName='buttonMaximum')layout.addWidget(self.buttonMaximum)# 关闭按钮self.buttonClose = QPushButton('r', self, clicked=self.windowClosed.emit, font=font, objectName='buttonClose')layout.addWidget(self.buttonClose)# 初始高度self.setHeight()def showMaximized(self):if self.buttonMaximum.text() == '1':# 最大化self.buttonMaximum.setText('2')self.windowMaximumed.emit()else:  # 还原self.buttonMaximum.setText('1')self.windowNormaled.emit()def setHeight(self, height=38):"""设置标题栏高度"""self.setMinimumHeight(height)self.setMaximumHeight(height)# 设置右边按钮的大小self.buttonMinimum.setMinimumSize(height, height)self.buttonMinimum.setMaximumSize(height, height)self.buttonMaximum.setMinimumSize(height, height)self.buttonMaximum.setMaximumSize(height, height)self.buttonClose.setMinimumSize(height, height)self.buttonClose.setMaximumSize(height, height)def setTitle(self, title):"""设置标题"""self.titleLabel.setText(title)def setIcon(self, icon):"""设置图标"""self.iconLabel.setPixmap(icon.pixmap(self.iconSize, self.iconSize))def setIconSize(self, size):"""设置图标大小"""self.iconSize = sizedef enterEvent(self, event):self.setCursor(Qt.ArrowCursor)super(TitleBar, self).enterEvent(event)def mouseDoubleClickEvent(self, event):super(TitleBar, self).mouseDoubleClickEvent(event)self.showMaximized()def mousePressEvent(self, event):"""鼠标点击事件"""if event.button() == Qt.LeftButton:self.mPos = event.pos()event.accept()def mouseReleaseEvent(self, event):'''鼠标弹起事件'''self.mPos = Noneevent.accept()def mouseMoveEvent(self, event):if event.buttons() == Qt.LeftButton and self.mPos:self.windowMoved.emit(self.mapToGlobal(event.pos() - self.mPos))event.accept()# 枚举左上右下以及四个定点
Left, Top, Right, Bottom, LeftTop, RightTop, LeftBottom, RightBottom = range(8)class FramelessWindow(QWidget):# 四周边距Margins = 5def __init__(self, *args, **kwargs):super(FramelessWindow, self).__init__(*args, **kwargs)palette1 = QtGui.QPalette()palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('./input/log0.jpg')))  # 设置登录背景图片self.setPalette(palette1)self.setAutoFillBackground(True)self.setGeometry(300, 300, 250, 150)self._pressed = Falseself.Direction = None# 无边框self.setWindowFlags(Qt.FramelessWindowHint)  # 隐藏边框# 鼠标跟踪self.setMouseTracking(True)# 布局layout = QVBoxLayout(self, spacing=0)layout.setContentsMargins(0,0,0,0)# 标题栏self.titleBar = TitleBar(self)layout.addWidget(self.titleBar)# 信号槽self.titleBar.windowMinimumed.connect(self.showMinimized)self.titleBar.windowMaximumed.connect(self.showMaximized)self.titleBar.windowNormaled.connect(self.showNormal)self.titleBar.windowClosed.connect(self.close)self.titleBar.windowMoved.connect(self.move)self.windowTitleChanged.connect(self.titleBar.setTitle)self.windowIconChanged.connect(self.titleBar.setIcon)#def setTitleBarHeight(self, height=38):def setTitleBarHeight(self, height=50):"""设置标题栏高度"""self.titleBar.setHeight(height)def setIconSize(self, size):"""设置图标的大小"""self.titleBar.setIconSize(size)def setWidget(self, widget):"""设置自己的控件"""if hasattr(self, '_widget'):returnself._widget = widget# 设置默认背景颜色,否则由于受到父窗口的影响导致透明self._widget.setAutoFillBackground(True)self._widget.installEventFilter(self)self.layout().addWidget(self._widget)def move(self, pos):if self.windowState() == Qt.WindowMaximized or self.windowState() == Qt.WindowFullScreen:# 最大化或者全屏则不允许移动returnsuper(FramelessWindow, self).move(pos)def showMaximized(self):"""最大化,要去除上下左右边界,如果不去除则边框地方会有空隙"""super(FramelessWindow, self).showMaximized()self.layout().setContentsMargins(0, 0, 0, 0)def showNormal(self):"""还原,要保留上下左右边界,否则没有边框无法调整"""super(FramelessWindow, self).showNormal()self.layout().setContentsMargins(0, 0, 0, 0)def eventFilter(self, obj, event):"""事件过滤器,用于解决鼠标进入其它控件后还原为标准鼠标样式"""if isinstance(event, QEnterEvent):self.setCursor(Qt.ArrowCursor)return super(FramelessWindow, self).eventFilter(obj, event)def paintEvent(self, event):"""由于是全透明背景窗口,重绘事件中绘制透明度为1的难以发现的边框,用于调整窗口大小"""super(FramelessWindow, self).paintEvent(event)painter = QPainter(self)painter.setPen(QPen(QColor(255, 255, 255, 1), 2 * self.Margins))painter.drawRect(self.rect())def mousePressEvent(self, event):"""鼠标点击事件"""super(FramelessWindow, self).mousePressEvent(event)if event.button() == Qt.LeftButton:self._mpos = event.pos()self._pressed = Truedef mouseReleaseEvent(self, event):'''鼠标弹起事件'''super(FramelessWindow, self).mouseReleaseEvent(event)self._pressed = Falseself.Direction = Nonedef mouseMoveEvent(self, event):"""鼠标移动事件"""super(FramelessWindow, self).mouseMoveEvent(event)pos = event.pos()xPos, yPos = pos.x(), pos.y()wm, hm = self.width() - self.Margins, self.height() - self.Marginsif self.isMaximized() or self.isFullScreen():self.Direction = Noneself.setCursor(Qt.ArrowCursor)returnif event.buttons() == Qt.LeftButton and self._pressed:self._resizeWidget(pos)returnif xPos <= self.Margins and yPos <= self.Margins:# 左上角self.Direction = LeftTopself.setCursor(Qt.SizeFDiagCursor)elif wm <= xPos <= self.width() and hm <= yPos <= self.height():# 右下角self.Direction = RightBottomself.setCursor(Qt.SizeFDiagCursor)elif wm <= xPos and yPos <= self.Margins:# 右上角self.Direction = RightTopself.setCursor(Qt.SizeBDiagCursor)elif xPos <= self.Margins and hm <= yPos:# 左下角self.Direction = LeftBottomself.setCursor(Qt.SizeBDiagCursor)elif 0 <= xPos <= self.Margins and self.Margins <= yPos <= hm:# 左边self.Direction = Leftself.setCursor(Qt.SizeHorCursor)elif wm <= xPos <= self.width() and self.Margins <= yPos <= hm:# 右边self.Direction = Rightself.setCursor(Qt.SizeHorCursor)elif self.Margins <= xPos <= wm and 0 <= yPos <= self.Margins:# 上面self.Direction = Topself.setCursor(Qt.SizeVerCursor)elif self.Margins <= xPos <= wm and hm <= yPos <= self.height():# 下面self.Direction = Bottomself.setCursor(Qt.SizeVerCursor)def _resizeWidget(self, pos):"""调整窗口大小"""if self.Direction == None:returnmpos = pos - self._mposxPos, yPos = mpos.x(), mpos.y()geometry = self.geometry()x, y, w, h = geometry.x(), geometry.y(), geometry.width(), geometry.height()if self.Direction == LeftTop:  # 左上角if w - xPos > self.minimumWidth():x += xPosw -= xPosif h - yPos > self.minimumHeight():y += yPosh -= yPoselif self.Direction == RightBottom:  # 右下角if w + xPos > self.minimumWidth():w += xPosself._mpos = posif h + yPos > self.minimumHeight():h += yPosself._mpos = poselif self.Direction == RightTop:  # 右上角if h - yPos > self.minimumHeight():y += yPosh -= yPosif w + xPos > self.minimumWidth():w += xPosself._mpos.setX(pos.x())elif self.Direction == LeftBottom:  # 左下角if w - xPos > self.minimumWidth():x += xPosw -= xPosif h + yPos > self.minimumHeight():h += yPosself._mpos.setY(pos.y())elif self.Direction == Left:  # 左边if w - xPos > self.minimumWidth():x += xPosw -= xPoselse:returnelif self.Direction == Right:  # 右边if w + xPos > self.minimumWidth():w += xPosself._mpos = poselse:returnelif self.Direction == Top:  # 上面if h - yPos > self.minimumHeight():y += yPosh -= yPoselse:returnelif self.Direction == Bottom:  # 下面if h + yPos > self.minimumHeight():h += yPosself._mpos = poselse:returnself.setGeometry(x, y, w, h)StyleSheet_2 = """
QComboBox{height: 20px;border-radius: 4px;border: 1px solid rgb(111, 156, 207);background: white;
}
QComboBox:enabled{color: grey;
}
QComboBox:!enabled {color: rgb(80, 80, 80);
}
QComboBox:enabled:hover, QComboBox:enabled:focus {color: rgb(51, 51, 51);
}
QComboBox::drop-down {background: transparent;
}
QComboBox::drop-down:hover {background: lightgrey;
}QComboBox QAbstractItemView {border: 1px solid rgb(111, 156, 207);background: white;outline: none;
}QLineEdit {border-radius: 4px;height: 20px;border: 1px solid rgb(111, 156, 207);background: white;
}
QLineEdit:enabled {color: rgb(84, 84, 84);
}
QLineEdit:enabled:hover, QLineEdit:enabled:focus {color: rgb(51, 51, 51);
}
QLineEdit:!enabled {color: rgb(80, 80, 80);
}"""   #QComobox和QLineEdite的样式StyleSheet_btn = """
QPushButton{height:30px;background-color: transparent;color: grey;border: 2px solid #555555;border-radius: 6px;}
QPushButton:hover {background-color: blue;border-radius: 6px;}
"""  #登录Button的样式class regist(QWidget):'''登录窗口'''def __init__(self, *args, **kwargs):super(regist, self).__init__()self._layout = QVBoxLayout(spacing=0)self._layout.setContentsMargins(0, 0, 0, 0)self.setAutoFillBackground(True)self.setWindowOpacity(0.9)#透明度self.setLayout(self._layout)self._setup_ui()def _setup_ui(self):self.main_layout = QGridLayout()self.main_layout.setAlignment(Qt.AlignCenter)name_label = QLabel('账号:')name_label.setStyleSheet("color:black;")#设置颜色name_label.setFont(QFont("SimSun", 13, 50))#设置字体及大小 第一个参数是字体(微软雅黑),第二个是字体大小,第三个是加粗(50代表正常)passwd_label = QLabel('密码:')passwd_label.setStyleSheet("color:black;")passwd_label.setFont(QFont("SimSun", 13, 50))self.name_box = QLineEdit()self.passwd_box = QLineEdit()self.passwd_box.setEchoMode(QLineEdit.Password)self.name_box.setStyleSheet(StyleSheet_2)self.passwd_box.setStyleSheet(StyleSheet_2)label = QLabel()login_btn = QPushButton("确认提交")login_btn.setStyleSheet(StyleSheet_btn)login_btn.setStyleSheet("color:red;")login_btn.setFont(QFont("Microsoft YaHei", 15, 50))self.main_layout.addWidget(name_label,0,0,1,1)    #坐标(0,0)的组件占用一行一列self.main_layout.addWidget(passwd_label,1,0,1,1)self.main_layout.addWidget(self.name_box,0,1,1,2)self.main_layout.addWidget(self.passwd_box,1,1,1, 2)self.main_layout.addWidget(label,3,0,1,3)self.main_layout.addWidget(login_btn,4,0,1,3)self._layout.addLayout(self.main_layout)self.main_layout.addWidget(name_label, 0, 0, 1, 1)  # 坐标(0,0)的组件占用一行一列self.main_layout.addWidget(passwd_label, 1, 0, 1, 1)self.main_layout.addWidget(self.name_box, 0, 1, 1, 2)self.main_layout.addWidget(self.passwd_box, 1, 1, 1, 2)self.main_layout.addWidget(label, 3, 0, 1, 3)self._layout.addLayout(self.main_layout)# 绑定按钮事件login_btn.clicked.connect(self.insert_Mysql_userinfo)# 获取连接def get_Mysql_conn(self):try:config = {"host": "localhost","user": "root","password": "root","database": "test_0505"}self.conn = pymysql.connect(**config)except pymysql.Error as e:print('Error: %s' % e)# 关闭连接def close_Mysql_conn(self):try:if self.conn:self.conn.close()except pymysql.Error as e:print('Error: %s' % e)# 获取用户信息(登录用)def get_Mysql_userinfo(self):self.get_Mysql_conn()sql = ' SELECT * FROM userinfo' #这里我的登录用户头叫userinfo ,每个人的数据库不同,在建立时初始化。# 使用cursor()方法获取操作游标cursor = self.conn.cursor()# 使用execute()方法执行SQL语句cursor.execute(sql)# 使用fetchall()方法获取全部数据result = cursor.fetchall()# 将数据用字典形式存储于resultresult = [dict(zip([k[0] for k in cursor.description], row)) for row in result]# 关闭连接cursor.close()self.close_Mysql_conn()return result# 注册def insert_Mysql_userinfo(self, text):print("进入用户注册提交函数22")self.name = str(self.name_box.text())self.pwd = str(self.passwd_box.text())self.get_Mysql_conn()#连接数据库sql = 'SELECT * FROM userinfo'cursor = self.conn.cursor()cursor.execute(sql)result = cursor.fetchall()print("result=", result)result = [dict(zip([k[0] for k in cursor.description], row)) for row in result]ulist = []for item in result:ulist.append(item['name'])print("ulist=", ulist)try:sql = "insert into userinfo(name, pwd) values('{}', '{}');".format(self.name,self.pwd)cursor.execute(sql)if self.name == '' or self.pwd == '':self.conn.rollback()QMessageBox.warning(self, "警告", "账号或者密码有空,注册失败", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)elif self.name in ulist:#messagebox.showerror('警告', message='用户名已存在')#msg_box = QMessageBox(QMessageBox.Warning, '警告', '用户名已存在')QMessageBox.warning(self, "警告", "用户名已存在", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)else:# 提交事务self.conn.commit()#messagebox.showinfo(title='恭喜', message='注册成功')#msg_box = QMessageBox(QMessageBox.information, '恭喜', '注册成功')QMessageBox.information(self, "恭喜", "注册成功", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)mainWnd2.close()  # 关闭注册界面,在这之后就可以进入你编写的其他界面了。cursor.close()self.close_conn()except:# 限制提交self.conn.rollback()#登录 本例没用def on_pushButton_Login_clicked(self, text):print("进入账号判断函数")self.get_Mysql_conn()  # 连接数据库sql = 'SELECT * FROM userinfo'cursor = self.conn.cursor()cursor.execute(sql)data = cursor.fetchall()# 打印获取到的数据for i in range(len(data)):#print(data[i][1])#print(data[i][2])if self.name_box.text() == data[i][1]:print("账号正确")if self.passwd_box.text() == data[i][2]:print("密码正确")#mainWnd.close()  # 关闭登陆界面,在这之后就可以进入你编写的其他界面了。cursor.close()self.close_conn()if __name__ == '__main__':app = QApplication(sys.argv)mainWnd2 = FramelessWindow()mainWnd2.setWindowTitle('注册')mainWnd2.setWindowIcon(QIcon('Qt.ico'))mainWnd2.setFixedSize(QSize(650, 500))  # 因为这里固定了大小,所以窗口的大小没有办法任意调整,想要使resizeWidget函数生效的话要把这里去掉,自己调节布局和窗口大小mainWnd2.setWidget(regist(mainWnd2))  # 把自己的窗口添加进来mainWnd2.show()app.exec()

python使用mysql数据库一个简单的 Pyqt5 用户注册系统相关推荐

  1. Python --链接MYSQL数据库与简单操作 含SSH链接

    项目是软硬件结合,在缺少设备的情况,需要通过接口来模拟实现与设备的交互,其中就需要通过从数据库读取商品的ID信息 出于安全考虑  现在很多数据库都不允许通过直接访问,大多数是通过SSH SSH : 数 ...

  2. Python结合MySQL数据库编写简单信息管理系统

    1,项目整体逻辑及使用工具 1.1 项目整体逻辑 本项目主要是使用Python进行编写,利用Python中的pymysql库进行连接数据库,将信息存入MySQL数据库中,然后实现对信息进行增删改查等一 ...

  3. php mysql 快餐_用PHP+MYSQL做一个简单的点餐系统的后台,初学者. 请高手指点下如何实现后台添加菜名 餐馆 价格...

    展开全部 "  name="cm"  placeholder="菜名" type="text" /> "  nam ...

  4. python数据库教程_Python连接mysql数据库及简单增删改查操作示例代码

    1.安装pymysql 进入cmd,输入 pip install pymysql: 2.数据库建表 在数据库中,建立一个简单的表,如图: 3.简单操作 3.1查询操作 #coding=utf-8 #连 ...

  5. python操作数据库教程_Python连接mysql数据库及简单增删改查操作示例代码

    1.安装pymysql 进入cmd,输入 pip install pymysql: 2.数据库建表 在数据库中,建立一个简单的表,如图: 3.简单操作 3.1查询操作 #coding=utf-8 #连 ...

  6. Python连接Mysql数据库实现图书借阅系统

    相信大家在学习python编程时绝对离不开数据库的连接,那么我们就用python来连接数据库实现一个简单的图书借阅系统.其实也很简单,就是在我们的程序中加入sql语句即可 数据库的表结构 我们在这里需 ...

  7. python使用pymysql连接mysql_python3使用PyMysql连接mysql数据库的简单示例

    这篇文章主要为大家详细介绍了python3使用PyMysql连接mysql数据库的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小 ...

  8. Python与MySQL数据库的交互实战

    作者 | Huang supreme 编辑 | 郭芮 图源 | 视觉中国 安装PyMySQL库 如果你想要使用python操作MySQL数据库,就必须先要安装pymysql库,这个库的安装很简单,直接 ...

  9. Python使用MySQL数据库(新)

    一,安装mysql 如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可. Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的l ...

最新文章

  1. C# DataGridView 的UserDeletingRow事件,删除
  2. bootstrap搜索框样式代码及效果
  3. Shell printf 命令
  4. 吴恩达的视频课程做成了文字版 ~~~
  5. BOOST使用 proto 转换进行任意类型操作的简单示例
  6. POJ 3648 Wedding
  7. 自己动手写CPU(7)转移指令的实现
  8. 【转载】WinCE中的RAM-Based Registry与HIVE-Based Registry
  9. python os.remove拒绝访问_「进阶Python」第八讲:代理模式
  10. aws rds监控慢sql_探索AWS RDS SQL Server上SQL Server集成服务(SSIS)
  11. 【WebGIS毕业设计】(一)前言、开题与参考文献
  12. 地震matlab频域分析,MATLAB在地震数据分析中的应用
  13. 全球高效能人士给青年的50个忠告(上) --转载
  14. 网络与系统安全笔记------身份认证技术
  15. Flutter开发:Another exception was thrown:Unable to load asset:…的解决方法
  16. VMware16.0如何装win7和win10
  17. HALCON_XLD轮廓算子_二
  18. matlab导入数据后画图_简易数据采集分析流程.stm32+python
  19. 隐私泄露下的数据暗网,分类标签中的爱恨一生
  20. 【每日随笔】毕业论文答辩 ① ( 答辩流程梳理 | 答辩的一些注意点 )

热门文章

  1. QT小案例之360UI模仿
  2. 如何禁止别人使用U盘的方法 - 并不需要使用专业软件
  3. matlab中mean的用法
  4. No timezone mapping entry for ‘Asia/Shanghai‘
  5. Simulink| “双碳”背景下汽车减少碳排放建模与仿真
  6. 笔记本外接显示器时打开Disney+出现黑屏,无信号输入。需要重新插拔HDMI线才可以显示
  7. 华3如何使用ftp和tftp进行文件传输与保存
  8. Win11怎么隐藏磁盘分区的方法教学
  9. 无规矩不成方圆,代码规范到底有多重要?(附阿里最新Android开发代码规范手册)
  10. springboot整合minio,实现文件上传与下载,且支持链接永久访问