-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalterPassword.py
More file actions
105 lines (94 loc) · 4.62 KB
/
alterPassword.py
File metadata and controls
105 lines (94 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Form implementation generated from reading ui file 'alterPassword.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(QtWidgets.QWidget):
def __init__(self, cursor, username):
super(Ui_Form, self).__init__()
self.cursor = cursor
self.username = username
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.formLayoutWidget = QtWidgets.QWidget(parent=Form)
self.formLayoutWidget.setGeometry(QtCore.QRect(20, 44, 351, 131))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setContentsMargins(10, 10, 10, 10)
self.formLayout.setSpacing(20)
self.formLayout.setObjectName("formLayout")
self.label_5 = QtWidgets.QLabel(parent=self.formLayoutWidget)
self.label_5.setObjectName("label_5")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_5)
self.oldPassword = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
self.oldPassword.setObjectName("oldPassword")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.ItemRole.FieldRole, self.oldPassword)
self.label_2 = QtWidgets.QLabel(parent=self.formLayoutWidget)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label_2)
self.newPassword = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
self.newPassword.setObjectName("newPassword")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.ItemRole.FieldRole, self.newPassword)
self.label = QtWidgets.QLabel(parent=self.formLayoutWidget)
self.label.setObjectName("label")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.ItemRole.LabelRole, self.label)
self.againPassword = QtWidgets.QLineEdit(parent=self.formLayoutWidget)
self.againPassword.setObjectName("againPassword")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.ItemRole.FieldRole, self.againPassword)
self.alterSure = QtWidgets.QPushButton(parent=Form)
self.alterSure.setGeometry(QtCore.QRect(290, 220, 81, 31))
self.alterSure.setObjectName("alterSure")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
# 确认修改按钮绑定事件
self.alterSure.clicked.connect(self.alterPassword)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "更换密码"))
self.label_5.setText(_translate("Form", "原密码:"))
self.label_2.setText(_translate("Form", "新密码:"))
self.label.setText(_translate("Form", "确认密码:"))
self.alterSure.setText(_translate("Form", "确认修改"))
# 修改密码函数
def alterPassword(self):
old_password = self.oldPassword.text()
new_password = self.newPassword.text()
again_password = self.againPassword.text()
if new_password != again_password:
QtWidgets.QMessageBox.warning(self, "警告", "两次输入的密码不一致!")
return
if not old_password or not new_password or not again_password:
QtWidgets.QMessageBox.warning(self, "警告", "密码不能为空!")
return
self.cursor.execute("SELECT password FROM user WHERE username = %s", (self.username,))
password = self.cursor.fetchone()[0]
if old_password != password:
QtWidgets.QMessageBox.warning(self, "警告", "原密码错误!")
return
if old_password == new_password:
QtWidgets.QMessageBox.warning(self, "警告", "新密码不能与原密码相同!")
return
self.cursor.execute("UPDATE user SET password = %s WHERE username = %s", (new_password, self.username))
QtWidgets.QMessageBox.information(self, "提示", "修改密码成功!")
self.cursor.connection.commit()
self.close()
if __name__ == "__main__":
import sys
import pymysql
app = QtWidgets.QApplication(sys.argv)
db = pymysql.connect(
host="localhost",
user="root",
password="10086",
database="library",
charset="utf8"
)
cursor = db.cursor()
username = "admin1"
ui = Ui_Form(cursor, username)
ui.show()
sys.exit(app.exec())