用python pyqt5界面开发写一段包含登录界面和修改个人信息界面和注销界面的python代码
时间: 2024-06-12 22:05:08 浏览: 200
这里提供一个简单的示例代码,仅供参考:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Login')
self.resize(300, 200)
self.label_username = QLabel('Username:', self)
self.label_username.move(50, 50)
self.edit_username = QLineEdit(self)
self.edit_username.move(120, 50)
self.label_password = QLabel('Password:', self)
self.label_password.move(50, 80)
self.edit_password = QLineEdit(self)
self.edit_password.setEchoMode(QLineEdit.Password)
self.edit_password.move(120, 80)
self.button_login = QPushButton('Login', self)
self.button_login.move(100, 130)
self.button_login.clicked.connect(self.handle_login)
self.show()
def handle_login(self):
username = self.edit_username.text()
password = self.edit_password.text()
if username == 'admin' and password == 'admin':
self.hide()
self.edit_username.setText('')
self.edit_password.setText('')
self.profile_window = ProfileWindow()
self.profile_window.show()
else:
QMessageBox.warning(self, 'Error', 'Invalid username or password!', QMessageBox.Ok)
class ProfileWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Profile')
self.resize(300, 200)
self.label_username = QLabel('Username:', self)
self.label_username.move(50, 50)
self.edit_username = QLineEdit(self)
self.edit_username.move(120, 50)
self.label_password = QLabel('Password:', self)
self.label_password.move(50, 80)
self.edit_password = QLineEdit(self)
self.edit_password.setEchoMode(QLineEdit.Password)
self.edit_password.move(120, 80)
self.button_update = QPushButton('Update', self)
self.button_update.move(100, 130)
self.button_update.clicked.connect(self.handle_update)
self.button_logout = QPushButton('Logout', self)
self.button_logout.move(180, 10)
self.button_logout.clicked.connect(self.handle_logout)
self.show()
def handle_update(self):
username = self.edit_username.text()
password = self.edit_password.text()
# TODO: update user info
def handle_logout(self):
self.hide()
self.login_window = LoginWindow()
self.login_window.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
login_window = LoginWindow()
sys.exit(app.exec_())
```
这个代码中有两个窗口,一个是登录窗口(`LoginWindow`),一个是个人信息窗口(`ProfileWindow`)。在登录窗口中,用户需要输入用户名和密码,如果登录成功,则跳转到个人信息窗口。在个人信息窗口中,用户可以修改个人信息,也可以注销并返回登录窗口。这里的代码还比较简单,实际项目中可能需要更复杂的逻辑处理。
阅读全文