为登录界面加上了登录注册功能,注册的用户会保存到一个txt文件中,在登录的时候根据输入的用户名在文件中查询数据
def login_in(self):
account = self.lineEdit_L_account.text()
password = self.lineEdit_L_password.text()
if not account or not password:
QMessageBox.warning(self, "Login Failed", "Username and password cannot be empty")
return
if self.check_credentials(account, password):
self.win = MainWindow(account) # 将账号传递给MainWindow
self.close()
else:
QMessageBox.warning(self, "Login Failed", "Incorrect username or password")
def register(self):
account = self.lineEdit_R_account.text()
password = self.lineEdit_R_password_2.text()
confirm_password = self.lineEdit_R_password_3.text()
if not account or not password or not confirm_password:
QMessageBox.warning(self, "Register Failed", "All fields are required")
return
if password != confirm_password:
QMessageBox.warning(self, "Register Failed", "Passwords do not match")
return
if self.save_credentials(account, password):
QMessageBox.information(self, "Register Success", "Registration successful! You can now log in.")
self.stackedWidget_2.setCurrentIndex(0)
else:
QMessageBox.warning(self, "Register Failed", "Username already exists")
def check_credentials(self, account, password):
try:
with open("users.txt", "r") as file:
for line in file:
if ',' in line:
user, passw = line.strip().split(",", 1)
if user == account and passw == password:
return True
except FileNotFoundError:
return False
return False
def save_credentials(self, account, password):
try:
with open("users.txt", "r") as file:
for line in file:
if ',' in line:
user, _ = line.strip().split(",", 1)
if user == account:
return False
except FileNotFoundError:
pass
with open("users.txt", "a") as file:
file.write(f"{account},{password}\n")
return True
根据需要将页面放大了尺寸并且加了拖拽功能
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self._startPos = event.pos()
self._isTracking = True
def mouseMoveEvent(self, event):
if self._isTracking:
self._endPos = event.pos() - self._startPos
self.move(self.pos() + self._endPos)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self._isTracking = False
self._startPos = None
self._endPos = None
根据要求添加了切换角色按钮和播放语音的按钮
聊天内容的气泡显示原理:
- 主窗口的布局是一个垂直布局(
QVBoxLayout
),其中包含一个可滚动区域(QScrollArea
)和两个水平布局(QHBoxLayout
)。 - 每个水平布局包含一个标签(
QLabel
)、一个文本输入框(QLineEdit
)、和两个按钮(QPushButton
),用于发送文本消息或图片。 - 定义了四个类:
RightBubblesTextCard
、RightBubblesPixCard
、LeftBubblesTextCard
和LeftBubblesPixCard
,分别用于右侧文本气泡、右侧图片气泡、左侧文本气泡和左侧图片气泡。这些类继承自QFrame
,并定义了各自的UI布局和样式。
styleThirdAreaButton = '''
QPushButton {
border: none;
border-radius: 8px;
background-color: #E2EEF8;
font-weight:300;
font-family: "Microsoft YaHei";
font-size: 16px;
}
QPushButton:hover {
background-color: #DBF2F9;
}
QPushButton:pressed {
background-color: #B5EBFC;
}
QPushButton:disabled {
border-radius: 8px;
background-color: #E7E7E7;
}
'''
styleLineEdit = '''
QLineEdit {
border: 2px solid #E7E7E7;
border-radius: 4px;
padding: 4 4px;
font-size:17px;
font-family:"Microsoft YaHei";
}
'''
修改密码功能
通过loginwindow传来的用户信息,找到密码位置再修改
def change_password(self):
new_password = self.lineEdit_M_pass_1.text()
confirm_new_password = self.lineEdit_M_pass_2.text()
if new_password != confirm_new_password:
QMessageBox.warning(self, "Change Password Failed", "New passwords do not match")
return
if self.update_password(self.username, new_password):
QMessageBox.information(self, "Success", "Password changed successfully")
else:
QMessageBox.warning(self, "Change Password Failed", "An error occurred while changing password")
def check_credentials(self, account, password):
try:
with open("users.txt", "r") as file:
for line in file:
if ',' in line:
user, passw = line.strip().split(",", 1)
if user == account and passw == password:
return True
except FileNotFoundError:
return False
return False