pyqt5登录界面设计——模仿qq登录界面,可登录注册(数据库)

淡淡的烟草味﹌ 2022-12-29 09:47 441阅读 0赞

pyqt5登录界面设计——模仿qq登录界面

  • 一、简单易用的可直接登录的界面——账号密码程序写死
    • 1.1、效果图:
    • 1.2、视频效果图
  • 3、代码说明
  • 4、使用示例
  • 5、全部源码见:
  • 二、带数据库的可登录注册
    • 效果图

今天给大家分享一个十分不错的pyqt5实现的登录界面,很像qq的登录界面,你可以将此登录界面用到自己的项目中,甚至完全不用修改,只需在自己项目中运行该py文件即可,在输入正确的账号和密码后,就可以进入的自己的主界面。

一、简单易用的可直接登录的界面——账号密码程序写死

1.1、效果图:

在这里插入图片描述

1.2、视频效果图

西瓜视频——pyqt5实现登录界面

3、代码说明

首先最开始,我们自己用代码编写了一个标题栏,用于美化我们的登录界面,但是这个和你主界面的并无关系。在代码190行第载入了一张图片,这个图片你可以随意改变。

  1. palette1.setBrush(self.backgroundRole(), QtGui.QBrush(
  2. QtGui.QPixmap('log0.jpg'))) # 设置登录背景图片

然后在536行,我们可以调整登录界面大小。

  1. mainWnd.setFixedSize(QSize(650,500)) #因为这里固定了大小,所以窗口的大小没有办法任意调整,想要使resizeWidget函数生效的话要把这里去掉,自己调节布局和窗口大小

在511行 on_pushButton_enter_clicked函数中,实现了一个账号和密码判断的函数,如果输入正确,则销毁登录界面,login_main.py函数运行结束,可以进入到你的主界面。这里的账号名和密码你可以随便改动

4、使用示例

假如我的原主界面函数如下:

  1. app = QtWidgets.QApplication(sys.argv)
  2. MainWindow = QtWidgets.QMainWindow()
  3. ui = Ui_MainWindow()
  4. ui.setupUi(MainWindow)
  5. MainWindow.show()
  6. sys.exit(app.exec_())

想加入登录界面,只需要在第二行直接插入

  1. os.system('python login_main.py') #执行login_main.py文件

  1. app = QtWidgets.QApplication(sys.argv)
  2. os.system('python login_main.py') #执行login_main.py文件,即登录界面
  3. MainWindow = QtWidgets.QMainWindow()
  4. ui = Ui_MainWindow()
  5. ui.setupUi(MainWindow)
  6. MainWindow.show()
  7. sys.exit(app.exec_())

就可以实现,登录界面,在输入正确的账号和密码后,就可以调到主界面。

5、全部源码见:

登录界面源代码

二、带数据库的可登录注册

效果图

在这里插入图片描述
在这里插入图片描述

  1. #login_main.py
  2. #!/usr/bin/env python
  3. # -*- coding:utf-8 -*-
  4. # Author: diyun
  5. # time:2020.9.7
  6. import pymysql
  7. from PyQt5.QtCore import Qt, pyqtSignal, QPoint
  8. from PyQt5.QtGui import QFont, QEnterEvent, QPainter, QColor, QPen
  9. from PyQt5.QtWidgets import QHBoxLayout, QLabel,QSpacerItem, QSizePolicy
  10. from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QTextEdit
  11. from PyQt5 import QtGui
  12. import sys
  13. from PyQt5.QtCore import QSize
  14. from PyQt5.QtWidgets import QApplication
  15. from PyQt5.QtCore import Qt
  16. from PyQt5.QtWidgets import QComboBox
  17. from PyQt5.QtWidgets import QGridLayout
  18. from PyQt5.QtWidgets import QLineEdit
  19. from PyQt5.QtWidgets import QLabel
  20. from PyQt5.QtGui import QIcon
  21. from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QTextEdit
  22. from PyQt5 import QtWidgets
  23. from PyQt5.QtWidgets import *
  24. from PyQt5.QtGui import *
  25. from PyQt5 import QtGui
  26. from PyQt5.QtGui import QFont
  27. StyleSheet = """ /*最小化最大化关闭按钮通用默认背景*/ #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; } """
  28. class TitleBar(QWidget):
  29. # 窗口最小化信号
  30. windowMinimumed = pyqtSignal()
  31. # 窗口最大化信号
  32. windowMaximumed = pyqtSignal()
  33. # 窗口还原信号
  34. windowNormaled = pyqtSignal()
  35. # 窗口关闭信号
  36. windowClosed = pyqtSignal()
  37. # 窗口移动
  38. windowMoved = pyqtSignal(QPoint)
  39. def __init__(self, *args, **kwargs):
  40. super(TitleBar, self).__init__(*args, **kwargs)
  41. self.setStyleSheet(StyleSheet)
  42. self.mPos = None
  43. self.iconSize = 20 # 图标的默认大小
  44. # 布局
  45. layout = QHBoxLayout(self, spacing=0)
  46. layout.setContentsMargins(0, 0, 0, 0)
  47. # 窗口图标
  48. self.iconLabel = QLabel(self)
  49. # self.iconLabel.setScaledContents(True)
  50. layout.addWidget(self.iconLabel)
  51. # 窗口标题
  52. self.titleLabel = QLabel(self)
  53. self.titleLabel.setStyleSheet("color:grey")
  54. self.titleLabel.setMargin(2)
  55. layout.addWidget(self.titleLabel)
  56. # 中间伸缩条
  57. layout.addSpacerItem(QSpacerItem(
  58. 40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
  59. # 利用Webdings字体来显示图标
  60. font = self.font() or QFont()
  61. font.setFamily('Webdings')
  62. # 最小化按钮
  63. self.buttonMinimum = QPushButton(
  64. '0', self, clicked=self.windowMinimumed.emit, font=font, objectName='buttonMinimum')
  65. layout.addWidget(self.buttonMinimum)
  66. # 最大化/还原按钮
  67. self.buttonMaximum = QPushButton(
  68. '1', self, clicked=self.showMaximized, font=font, objectName='buttonMaximum')
  69. layout.addWidget(self.buttonMaximum)
  70. # 关闭按钮
  71. self.buttonClose = QPushButton(
  72. 'r', self, clicked=self.windowClosed.emit, font=font, objectName='buttonClose')
  73. layout.addWidget(self.buttonClose)
  74. # 初始高度
  75. self.setHeight()
  76. def showMaximized(self):
  77. if self.buttonMaximum.text() == '1':
  78. # 最大化
  79. self.buttonMaximum.setText('2')
  80. self.windowMaximumed.emit()
  81. else: # 还原
  82. self.buttonMaximum.setText('1')
  83. self.windowNormaled.emit()
  84. def setHeight(self, height=38):
  85. """设置标题栏高度"""
  86. self.setMinimumHeight(height)
  87. self.setMaximumHeight(height)
  88. # 设置右边按钮的大小
  89. self.buttonMinimum.setMinimumSize(height, height)
  90. self.buttonMinimum.setMaximumSize(height, height)
  91. self.buttonMaximum.setMinimumSize(height, height)
  92. self.buttonMaximum.setMaximumSize(height, height)
  93. self.buttonClose.setMinimumSize(height, height)
  94. self.buttonClose.setMaximumSize(height, height)
  95. def setTitle(self, title):
  96. """设置标题"""
  97. self.titleLabel.setText(title)
  98. def setIcon(self, icon):
  99. """设置图标"""
  100. self.iconLabel.setPixmap(icon.pixmap(self.iconSize, self.iconSize))
  101. def setIconSize(self, size):
  102. """设置图标大小"""
  103. self.iconSize = size
  104. def enterEvent(self, event):
  105. self.setCursor(Qt.ArrowCursor)
  106. super(TitleBar, self).enterEvent(event)
  107. def mouseDoubleClickEvent(self, event):
  108. super(TitleBar, self).mouseDoubleClickEvent(event)
  109. self.showMaximized()
  110. def mousePressEvent(self, event):
  111. """鼠标点击事件"""
  112. if event.button() == Qt.LeftButton:
  113. self.mPos = event.pos()
  114. event.accept()
  115. def mouseReleaseEvent(self, event):
  116. '''鼠标弹起事件'''
  117. self.mPos = None
  118. event.accept()
  119. def mouseMoveEvent(self, event):
  120. if event.buttons() == Qt.LeftButton and self.mPos:
  121. self.windowMoved.emit(self.mapToGlobal(event.pos() - self.mPos))
  122. event.accept()
  123. # 枚举左上右下以及四个定点
  124. Left, Top, Right, Bottom, LeftTop, RightTop, LeftBottom, RightBottom = range(8)
  125. class FramelessWindow(QWidget):
  126. # 四周边距
  127. Margins = 5
  128. def __init__(self, *args, **kwargs):
  129. super(FramelessWindow, self).__init__(*args, **kwargs)
  130. palette1 = QtGui.QPalette()
  131. palette1.setBrush(self.backgroundRole(), QtGui.QBrush(
  132. QtGui.QPixmap('./input/log0.jpg'))) # 设置登录背景图片
  133. self.setPalette(palette1)
  134. self.setAutoFillBackground(True)
  135. self.setGeometry(300, 300, 250, 150)
  136. self._pressed = False
  137. self.Direction = None
  138. # 无边框
  139. self.setWindowFlags(Qt.FramelessWindowHint) # 隐藏边框
  140. # 鼠标跟踪
  141. self.setMouseTracking(True)
  142. # 布局
  143. layout = QVBoxLayout(self, spacing=0)
  144. layout.setContentsMargins(0,0,0,0)
  145. # 标题栏
  146. self.titleBar = TitleBar(self)
  147. layout.addWidget(self.titleBar)
  148. # 信号槽
  149. self.titleBar.windowMinimumed.connect(self.showMinimized)
  150. self.titleBar.windowMaximumed.connect(self.showMaximized)
  151. self.titleBar.windowNormaled.connect(self.showNormal)
  152. self.titleBar.windowClosed.connect(self.close)
  153. self.titleBar.windowMoved.connect(self.move)
  154. self.windowTitleChanged.connect(self.titleBar.setTitle)
  155. self.windowIconChanged.connect(self.titleBar.setIcon)
  156. #def setTitleBarHeight(self, height=38):
  157. def setTitleBarHeight(self, height=50):
  158. """设置标题栏高度"""
  159. self.titleBar.setHeight(height)
  160. def setIconSize(self, size):
  161. """设置图标的大小"""
  162. self.titleBar.setIconSize(size)
  163. def setWidget(self, widget):
  164. """设置自己的控件"""
  165. if hasattr(self, '_widget'):
  166. return
  167. self._widget = widget
  168. # 设置默认背景颜色,否则由于受到父窗口的影响导致透明
  169. self._widget.setAutoFillBackground(True)
  170. self._widget.installEventFilter(self)
  171. self.layout().addWidget(self._widget)
  172. def move(self, pos):
  173. if self.windowState() == Qt.WindowMaximized or self.windowState() == Qt.WindowFullScreen:
  174. # 最大化或者全屏则不允许移动
  175. return
  176. super(FramelessWindow, self).move(pos)
  177. def showMaximized(self):
  178. """最大化,要去除上下左右边界,如果不去除则边框地方会有空隙"""
  179. super(FramelessWindow, self).showMaximized()
  180. self.layout().setContentsMargins(0, 0, 0, 0)
  181. def showNormal(self):
  182. """还原,要保留上下左右边界,否则没有边框无法调整"""
  183. super(FramelessWindow, self).showNormal()
  184. self.layout().setContentsMargins(0, 0, 0, 0)
  185. def eventFilter(self, obj, event):
  186. """事件过滤器,用于解决鼠标进入其它控件后还原为标准鼠标样式"""
  187. if isinstance(event, QEnterEvent):
  188. self.setCursor(Qt.ArrowCursor)
  189. return super(FramelessWindow, self).eventFilter(obj, event)
  190. def paintEvent(self, event):
  191. """由于是全透明背景窗口,重绘事件中绘制透明度为1的难以发现的边框,用于调整窗口大小"""
  192. super(FramelessWindow, self).paintEvent(event)
  193. painter = QPainter(self)
  194. painter.setPen(QPen(QColor(255, 255, 255, 1), 2 * self.Margins))
  195. painter.drawRect(self.rect())
  196. def mousePressEvent(self, event):
  197. """鼠标点击事件"""
  198. super(FramelessWindow, self).mousePressEvent(event)
  199. if event.button() == Qt.LeftButton:
  200. self._mpos = event.pos()
  201. self._pressed = True
  202. def mouseReleaseEvent(self, event):
  203. '''鼠标弹起事件'''
  204. super(FramelessWindow, self).mouseReleaseEvent(event)
  205. self._pressed = False
  206. self.Direction = None
  207. def mouseMoveEvent(self, event):
  208. """鼠标移动事件"""
  209. super(FramelessWindow, self).mouseMoveEvent(event)
  210. pos = event.pos()
  211. xPos, yPos = pos.x(), pos.y()
  212. wm, hm = self.width() - self.Margins, self.height() - self.Margins
  213. if self.isMaximized() or self.isFullScreen():
  214. self.Direction = None
  215. self.setCursor(Qt.ArrowCursor)
  216. return
  217. if event.buttons() == Qt.LeftButton and self._pressed:
  218. self._resizeWidget(pos)
  219. return
  220. if xPos <= self.Margins and yPos <= self.Margins:
  221. # 左上角
  222. self.Direction = LeftTop
  223. self.setCursor(Qt.SizeFDiagCursor)
  224. elif wm <= xPos <= self.width() and hm <= yPos <= self.height():
  225. # 右下角
  226. self.Direction = RightBottom
  227. self.setCursor(Qt.SizeFDiagCursor)
  228. elif wm <= xPos and yPos <= self.Margins:
  229. # 右上角
  230. self.Direction = RightTop
  231. self.setCursor(Qt.SizeBDiagCursor)
  232. elif xPos <= self.Margins and hm <= yPos:
  233. # 左下角
  234. self.Direction = LeftBottom
  235. self.setCursor(Qt.SizeBDiagCursor)
  236. elif 0 <= xPos <= self.Margins and self.Margins <= yPos <= hm:
  237. # 左边
  238. self.Direction = Left
  239. self.setCursor(Qt.SizeHorCursor)
  240. elif wm <= xPos <= self.width() and self.Margins <= yPos <= hm:
  241. # 右边
  242. self.Direction = Right
  243. self.setCursor(Qt.SizeHorCursor)
  244. elif self.Margins <= xPos <= wm and 0 <= yPos <= self.Margins:
  245. # 上面
  246. self.Direction = Top
  247. self.setCursor(Qt.SizeVerCursor)
  248. elif self.Margins <= xPos <= wm and hm <= yPos <= self.height():
  249. # 下面
  250. self.Direction = Bottom
  251. self.setCursor(Qt.SizeVerCursor)
  252. def _resizeWidget(self, pos):
  253. """调整窗口大小"""
  254. if self.Direction == None:
  255. return
  256. mpos = pos - self._mpos
  257. xPos, yPos = mpos.x(), mpos.y()
  258. geometry = self.geometry()
  259. x, y, w, h = geometry.x(), geometry.y(), geometry.width(), geometry.height()
  260. if self.Direction == LeftTop: # 左上角
  261. if w - xPos > self.minimumWidth():
  262. x += xPos
  263. w -= xPos
  264. if h - yPos > self.minimumHeight():
  265. y += yPos
  266. h -= yPos
  267. elif self.Direction == RightBottom: # 右下角
  268. if w + xPos > self.minimumWidth():
  269. w += xPos
  270. self._mpos = pos
  271. if h + yPos > self.minimumHeight():
  272. h += yPos
  273. self._mpos = pos
  274. elif self.Direction == RightTop: # 右上角
  275. if h - yPos > self.minimumHeight():
  276. y += yPos
  277. h -= yPos
  278. if w + xPos > self.minimumWidth():
  279. w += xPos
  280. self._mpos.setX(pos.x())
  281. elif self.Direction == LeftBottom: # 左下角
  282. if w - xPos > self.minimumWidth():
  283. x += xPos
  284. w -= xPos
  285. if h + yPos > self.minimumHeight():
  286. h += yPos
  287. self._mpos.setY(pos.y())
  288. elif self.Direction == Left: # 左边
  289. if w - xPos > self.minimumWidth():
  290. x += xPos
  291. w -= xPos
  292. else:
  293. return
  294. elif self.Direction == Right: # 右边
  295. if w + xPos > self.minimumWidth():
  296. w += xPos
  297. self._mpos = pos
  298. else:
  299. return
  300. elif self.Direction == Top: # 上面
  301. if h - yPos > self.minimumHeight():
  302. y += yPos
  303. h -= yPos
  304. else:
  305. return
  306. elif self.Direction == Bottom: # 下面
  307. if h + yPos > self.minimumHeight():
  308. h += yPos
  309. self._mpos = pos
  310. else:
  311. return
  312. self.setGeometry(x, y, w, h)
  313. 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的样式
  314. 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的样式
  315. class loginWnd(QWidget):
  316. '''登录窗口'''
  317. def __init__(self, *args, **kwargs):
  318. super(loginWnd, self).__init__()
  319. self._layout = QVBoxLayout(spacing=0)
  320. self._layout.setContentsMargins(0, 0, 0, 0)
  321. self.setAutoFillBackground(True)
  322. self.setWindowOpacity(0.9)#透明度
  323. self.setLayout(self._layout)
  324. self._setup_ui()
  325. def _setup_ui(self):
  326. self.main_layout = QGridLayout()
  327. self.main_layout.setAlignment(Qt.AlignCenter)
  328. name_label = QLabel('账号:')
  329. name_label.setStyleSheet("color:black;")#设置颜色
  330. name_label.setFont(QFont("SimSun", 13, 50))#设置字体及大小 第一个参数是字体(微软雅黑),第二个是字体大小,第三个是加粗(50代表正常)
  331. passwd_label = QLabel('密码:')
  332. passwd_label.setStyleSheet("color:black;")
  333. passwd_label.setFont(QFont("SimSun", 13, 50))
  334. # self.name_box = QComboBox()
  335. # self.name_box.setEditable(True)
  336. self.name_box = QLineEdit()
  337. #self.name_box.setEchoMode(QLineEdit.Password)
  338. #self.name_box.setEditable(True)
  339. self.passwd_box = QLineEdit()
  340. self.passwd_box.setEchoMode(QLineEdit.Password)
  341. self.name_box.setStyleSheet(StyleSheet_2)
  342. self.passwd_box.setStyleSheet(StyleSheet_2)
  343. label = QLabel()
  344. login_btn = QPushButton("登录")
  345. login_btn.setStyleSheet(StyleSheet_btn)
  346. login_btn.setStyleSheet("color:red;")
  347. login_btn.setFont(QFont("Microsoft YaHei", 15, 50))
  348. self.main_layout.addWidget(name_label,0,0,1,1) #坐标(0,0)的组件占用一行一列
  349. self.main_layout.addWidget(passwd_label,1,0,1,1)
  350. self.main_layout.addWidget(self.name_box,0,1,1,2)
  351. self.main_layout.addWidget(self.passwd_box,1,1,1, 2)
  352. self.main_layout.addWidget(label,3,0,1,3)
  353. self.main_layout.addWidget(login_btn,4,0,1,3)
  354. self._layout.addLayout(self.main_layout)
  355. ###### 绑定按钮事件
  356. login_btn.clicked.connect(self.on_pushButton_enter_clicked)
  357. regist_btn = QPushButton("注册")
  358. regist_btn.setStyleSheet(StyleSheet_btn)
  359. regist_btn.setStyleSheet("color:red;")
  360. regist_btn.setFont(QFont("Microsoft YaHei", 15, 50))
  361. self.main_layout.addWidget(name_label, 0, 0, 1, 1) # 坐标(0,0)的组件占用一行一列
  362. self.main_layout.addWidget(passwd_label, 1, 0, 1, 1)
  363. self.main_layout.addWidget(self.name_box, 0, 1, 1, 2)
  364. self.main_layout.addWidget(self.passwd_box, 1, 1, 1, 2)
  365. self.main_layout.addWidget(label, 3, 0, 1, 3)
  366. self.main_layout.addWidget(regist_btn, 5, 0, 1, 3)
  367. self._layout.addLayout(self.main_layout)
  368. ###### 绑定按钮事件
  369. login_btn.clicked.connect(self.on_pushButton_enter_clicked)
  370. regist_btn.clicked.connect(self.on_pushButton_regist_clicked)
  371. def on_pushButton_regist_clicked(self, text):
  372. print("进入注册界面")
  373. mainWnd.hide() # 关闭登陆界面,在这之后就可以进入你编写的其他界面了
  374. mainWnd2.show()
  375. def on_pushButton_enter_clicked(self, text):
  376. print("进入账号判断函数")
  377. db = pymysql.connect(host='localhost', user='root', password='root', charset='utf8mb4', db='fire')
  378. #这里要提前在数据库中新建相关的表格
  379. # 使用cursor()方法获取操作游标
  380. cursor = db.cursor()
  381. # SQL 查询语句
  382. cursor.execute("SELECT * FROM userinfo")
  383. # 使用fetall()获取全部数据
  384. data = cursor.fetchall()
  385. # 打印获取到的数据
  386. for i in range(len(data)):
  387. print(data[i][1])
  388. print(data[i][2])
  389. if self.name_box.text() == data[i][1]:
  390. print("账号正确")
  391. if self.passwd_box.text() == data[i][2]:
  392. print("密码正确")
  393. mainWnd.close() # 关闭登陆界面,在这之后就可以进入你编写的其他界面了。
  394. cursor.close()
  395. db.close()
  396. class regist(QWidget):
  397. '''登录窗口'''
  398. def __init__(self, *args, **kwargs):
  399. super(regist, self).__init__()
  400. self._layout = QVBoxLayout(spacing=0)
  401. self._layout.setContentsMargins(0, 0, 0, 0)
  402. self.setAutoFillBackground(True)
  403. self.setWindowOpacity(0.9)#透明度
  404. self.setLayout(self._layout)
  405. self._setup_ui()
  406. def _setup_ui(self):
  407. self.main_layout = QGridLayout()
  408. self.main_layout.setAlignment(Qt.AlignCenter)
  409. name_label = QLabel('账号:')
  410. name_label.setStyleSheet("color:black;")#设置颜色
  411. name_label.setFont(QFont("SimSun", 13, 50))#设置字体及大小 第一个参数是字体(微软雅黑),第二个是字体大小,第三个是加粗(50代表正常)
  412. passwd_label = QLabel('密码:')
  413. passwd_label.setStyleSheet("color:black;")
  414. passwd_label.setFont(QFont("SimSun", 13, 50))
  415. # self.name_box = QComboBox()
  416. # self.name_box.setEditable(True)
  417. self.name_box = QLineEdit()
  418. #self.name_box.setEchoMode(QLineEdit.Password)
  419. #self.name_box.setEditable(True)
  420. self.passwd_box = QLineEdit()
  421. self.passwd_box.setEchoMode(QLineEdit.Password)
  422. self.name_box.setStyleSheet(StyleSheet_2)
  423. self.passwd_box.setStyleSheet(StyleSheet_2)
  424. label = QLabel()
  425. login_btn = QPushButton("确认提交")
  426. login_btn.setStyleSheet(StyleSheet_btn)
  427. login_btn.setStyleSheet("color:red;")
  428. login_btn.setFont(QFont("Microsoft YaHei", 15, 50))
  429. self.main_layout.addWidget(name_label,0,0,1,1) #坐标(0,0)的组件占用一行一列
  430. self.main_layout.addWidget(passwd_label,1,0,1,1)
  431. self.main_layout.addWidget(self.name_box,0,1,1,2)
  432. self.main_layout.addWidget(self.passwd_box,1,1,1, 2)
  433. self.main_layout.addWidget(label,3,0,1,3)
  434. self.main_layout.addWidget(login_btn,4,0,1,3)
  435. self._layout.addLayout(self.main_layout)
  436. ###### 绑定按钮事件
  437. regist_btn = QPushButton("返回登录")
  438. regist_btn.setStyleSheet(StyleSheet_btn)
  439. regist_btn.setStyleSheet("color:red;")
  440. regist_btn.setFont(QFont("Microsoft YaHei", 15, 50))
  441. self.main_layout.addWidget(name_label, 0, 0, 1, 1) # 坐标(0,0)的组件占用一行一列
  442. self.main_layout.addWidget(passwd_label, 1, 0, 1, 1)
  443. self.main_layout.addWidget(self.name_box, 0, 1, 1, 2)
  444. self.main_layout.addWidget(self.passwd_box, 1, 1, 1, 2)
  445. self.main_layout.addWidget(label, 3, 0, 1, 3)
  446. self.main_layout.addWidget(regist_btn, 5, 0, 1, 3)
  447. self._layout.addLayout(self.main_layout)
  448. ###### 绑定按钮事件
  449. login_btn.clicked.connect(self.on_pushButton_tijiao_clicked)
  450. regist_btn.clicked.connect(self.on_pushButton_fanhui_clicked)
  451. def on_pushButton_fanhui_clicked(self, text):
  452. print("进入登录界面")
  453. mainWnd2.hide() # 关闭登陆界面,在这之后就可以进入你编写的其他界面了
  454. mainWnd.show()
  455. def on_pushButton_regist_clicked(self, text):
  456. print("进入注册界面")
  457. mainWnd.hide() # 关闭登陆界面,在这之后就可以进入你编写的其他界面了
  458. mainWnd2.show()
  459. def on_pushButton_tijiao_clicked(self, text):
  460. config = {
  461. "host": "localhost",
  462. "user": "root",
  463. "password": "root",
  464. "database": "fire"
  465. }
  466. db = pymysql.connect(**config)
  467. cursor = db.cursor()
  468. name=str(self.name_box.text())
  469. password=str(self.passwd_box.text())
  470. book_num=0
  471. if name!='null':
  472. sql = "insert into userinfo values({}, '{}', '{}');".format(book_num,name,password)
  473. cursor.execute(sql)
  474. db.commit() # 提交数据
  475. cursor.close()
  476. db.close()
  477. print("注册成功")
  478. def on_pushButton_enter_clicked(self, text):
  479. print("进入账号判断函数")
  480. db = pymysql.connect(host='localhost', user='root', password='root', charset='utf8mb4', db='fire')
  481. #这里要提前在数据库中新建相关的表格
  482. # 使用cursor()方法获取操作游标
  483. cursor = db.cursor()
  484. # SQL 查询语句
  485. cursor.execute("SELECT * FROM userinfo")
  486. # 使用fetall()获取全部数据
  487. data = cursor.fetchall()
  488. # 打印获取到的数据
  489. for i in range(len(data)):
  490. #print(data[i][1])
  491. #print(data[i][2])
  492. if self.name_box.text() == data[i][1]:
  493. print("账号正确")
  494. if self.passwd_box.text() == data[i][2]:
  495. print("密码正确")
  496. mainWnd.close() # 关闭登陆界面,在这之后就可以进入你编写的其他界面了。
  497. cursor.close()
  498. db.close()
  499. if __name__ == '__main__':
  500. app = QApplication(sys.argv)
  501. mainWnd = FramelessWindow()
  502. mainWnd.setWindowTitle('登录')
  503. mainWnd.setWindowIcon(QIcon('Qt.ico'))
  504. mainWnd.setFixedSize(QSize(650,500)) #因为这里固定了大小,所以窗口的大小没有办法任意调整,想要使resizeWidget函数生效的话要把这里去掉,自己调节布局和窗口大小
  505. mainWnd.setWidget(loginWnd(mainWnd)) # 把自己的窗口添加进来
  506. mainWnd.show()
  507. mainWnd2 = FramelessWindow()
  508. mainWnd2.setWindowTitle('注册')
  509. mainWnd2.setWindowIcon(QIcon('Qt.ico'))
  510. mainWnd2.setFixedSize(QSize(650, 500)) # 因为这里固定了大小,所以窗口的大小没有办法任意调整,想要使resizeWidget函数生效的话要把这里去掉,自己调节布局和窗口大小
  511. mainWnd2.setWidget(regist(mainWnd2)) # 把自己的窗口添加进来
  512. mainWnd2.hide()
  513. app.exec()

发表评论

表情:
评论列表 (有 0 条评论,441人围观)

还没有评论,来说两句吧...

相关阅读

    相关 QQ聊天室登录界面

    界面布局 分上中下三个部分。顶部是一张图,最下面是三个按钮,中间就是qq号码框和密码框,在加上几个按钮。 ![在这里插入图片描述][watermark_type_Zm

    相关 PyQt5设置登录界面界面美化

    写在前面 前一段时间博主刷了半个多月的LeetCode算法题,刷的归类为简单的那些题,做到自己简直要怀疑人生。想着人生在世,何苦这么为难自己呢,何不做点自己擅长的东西。想

    相关 django做一个注册登录界面

    一、基于sb-admin模板完成以下登录的逻辑 用户想要访问一个网页,首先要求注册账号,有了账号之后再去登录,才能访问网站的主页 1.先说以下用户注册的逻辑: r