
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox, QFontDialog, QColorDialog, QFileDialog
from PyQt6.QtCore import Qt
from PyQt6.uic import loadUi
from PyQt6.QtGui import QFont, QColor
from PyQt6.QtWidgets import QApplication, QMessageBox, QFontDialog
from PyQt6 import uic
import sysfrom PyQt6.QtCore import QFile, QIODeviceBase, QByteArray
from PyQt6.QtGui import QFont, QColor
from PyQt6.QtWidgets import QApplication, QMessageBox, QFontDialog, QColorDialog, QFileDialog
from PyQt6 import uic
class LoginWindow(QMainWindow):def __init__(self):super().__init__()loadUi("./login.ui", self) # 加载UI文件# 连接信号和槽self.pushButton_login.clicked.connect(self.login_slot)self.pushButton_cancel.clicked.connect(self.cancel_slot)self.pushButton3.clicked.connect(self.font_slot)self.pushButton4.clicked.connect(self.color_slot)self.pushButton5.clicked.connect(self.open_slot)self.pushButton6.clicked.connect(self.save_slot)def login_slot(self):# 获取输入的账号和密码username = self.lineEdit_username.text()password = self.lineEdit_password.text()# 判断账号和密码是否正确if username == "admin" and password == "123456":# 账号密码正确,关闭当前界面并打开另一个界面(此处为示例,实际需自行实现)self.close()# QMessageBox.information(self, "登录成功", "欢迎,admin!") # 已在原代码中实现,可注释或删除# 示例:弹出信息对话框(已在原代码中实现,此处不再重复)else:# 账号密码错误,显示错误消息并清空输入框QMessageBox.warning(self, "登录失败", "账号或密码错误,请重试。")self.lineEdit_username.clear()self.lineEdit_password.clear()def cancel_slot(self):# 取消按钮被点击,关闭当前界面self.close()# 字体按钮对应的槽函数def font_slot(self):# 弹出字体对话框''' getFont(initial: QFont, parent: typing.Optional[QWidget] = None, caption: str = '', options: QFontDialog.FontDialogOption = QFontDialog.FontDialogOptions()) -> Tuple[QFont, bool]'''font, ok = QFontDialog.getFont(QFont("宋体", 8, 10), # 初始字体self, # 指定父组件"字体对话框" # 对话框标题)# 判断用户是否选中字体if ok:# 用户选中了字体# ui界面设置字体# ui.textEdit.setFont(font) #将文本全部设置成该字体self.textEdit.setCurrentFont(font) # 将当前文本设置成该字体else:# 用户没有选中字体QMessageBox.information(self, "", "你没有选中字体")# 颜色按钮对应的槽函数def color_slot(self):# 弹出颜色对话框""" getColor(initial: Union[QColor, Qt.GlobalColor, int, QGradient] = Qt.white, parent: typing.Optional[QWidget] = None, title: str = '', options: QColorDialog.ColorDialogOption = QColorDialog.ColorDialogOptions()) -> QColor """color = QColorDialog.getColor(QColor(255, 0, 0), # 初始颜色self, # 指定父组件"颜色对话框" # 对话框的标题)# 判断用户是否选中有效颜色if color.isValid():# 说明用户选中了颜色# 将ui界面上的文本设置改颜色# ui.textEdit.setTextColor(color) #文本的前景色self.textEdit.setTextBackgroundColor(color) # 文本的背景色else:# 说明用户没有选中颜色QMessageBox.information(self, "", "没有选中颜色")# 打开按钮对应的槽函数def open_slot(self):# 弹出打开文件对话框""" getOpenFileName(parent: typing.Optional[QWidget] = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: QFileDialog.Option = QFileDialog.Options()) -> Tuple[str, str] """fileName, fileType = QFileDialog.getOpenFileName(self, # 父组件"打开文件", # 标题"./", # 起始路径"All(*.*);;Image(*.png *.jpg *ico);;文本(*.txt)" # 过滤器)# print(fileName)# 实例化一个文件对象file = QFile(fileName) # file ==> XXX。txt# 打开文件file.open(QIODeviceBase.OpenModeFlag.ReadWrite)# 读取数据msg = file.readAll() # 返回的是数据 且类型是QByteArray类型# 将读取到的数据放入ui界面上self.textEdit.setText(msg.data().decode('utf-8')) # 类型转换 将QByteArray转换成str类型# 关闭文件file.close()# 保存按钮对应的槽函数def save_slot(self):# 弹出另存为的文件对话框fileName, fileType = QFileDialog.getSaveFileName()# 用QFile实例化一个文件对象file = QFile(fileName)# 打开文件file.open(QIODeviceBase.OpenModeFlag.WriteOnly) # 以只写的方式打开# 获取ui界面上文本msg = self.textEdit.toPlainText() # ==text() msg 是str# 写入数据byte_array_msg = QByteArray(msg.encode('utf-8')) # 将字符串转换成QByteArrayfile.write(byte_array_msg)# 关闭文件file.close()if __name__ == "__main__":app = QApplication(sys.argv)login_window = LoginWindow()login_window.show()sys.exit(app.exec())