用python pyqt5界面开发写一段包含登录界面和修改个人信息界面的python代码
时间: 2024-06-12 17:05:08 浏览: 14
以下是一个基本的登录界面和修改个人信息界面的Python代码,使用PyQt5库进行GUI开发:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QHBoxLayout, QVBoxLayout, QMessageBox
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口大小和标题
self.setGeometry(300, 300, 400, 200)
self.setWindowTitle('Login')
# 创建控件
self.username_label = QLabel('Username:')
self.username_edit = QLineEdit()
self.password_label = QLabel('Password:')
self.password_edit = QLineEdit()
self.password_edit.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton('Login')
self.login_button.clicked.connect(self.login)
# 设置布局
vbox = QVBoxLayout()
hbox1 = QHBoxLayout()
hbox1.addWidget(self.username_label)
hbox1.addWidget(self.username_edit)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.password_label)
hbox2.addWidget(self.password_edit)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addWidget(self.login_button)
self.setLayout(vbox)
def login(self):
# 获取用户名和密码
username = self.username_edit.text()
password = self.password_edit.text()
# 检查用户名和密码是否正确
if username == 'admin' and password == 'password':
# 登录成功,打开个人信息窗口
self.personal_info_window = PersonalInfoWindow()
self.personal_info_window.show()
self.close()
else:
# 登录失败,弹出错误提示框
QMessageBox.warning(self, 'Error', 'Incorrect username or password!')
class PersonalInfoWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口大小和标题
self.setGeometry(300, 300, 400, 200)
self.setWindowTitle('Personal Information')
# 创建控件
self.username_label = QLabel('Username:')
self.username_edit = QLineEdit()
self.password_label = QLabel('Password:')
self.password_edit = QLineEdit()
self.password_edit.setEchoMode(QLineEdit.Password)
self.save_button = QPushButton('Save')
self.save_button.clicked.connect(self.save)
# 设置布局
vbox = QVBoxLayout()
hbox1 = QHBoxLayout()
hbox1.addWidget(self.username_label)
hbox1.addWidget(self.username_edit)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.password_label)
hbox2.addWidget(self.password_edit)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addWidget(self.save_button)
self.setLayout(vbox)
def save(self):
# 保存个人信息
username = self.username_edit.text()
password = self.password_edit.text()
# 这里可以将个人信息保存到文件或数据库中
# ...
# 提示保存成功
QMessageBox.information(self, 'Success', 'Personal information saved successfully!')
if __name__ == '__main__':
app = QApplication(sys.argv)
login_window = LoginWindow()
login_window.show()
sys.exit(app.exec_())
```
这个程序包含一个登录窗口和一个个人信息窗口。用户在登录窗口中输入用户名和密码,程序验证用户名和密码是否正确,如果正确,打开个人信息窗口,在个人信息窗口中可以修改用户名和密码,并保存到文件或数据库中。程序使用PyQt5库进行GUI开发,使用了QLabel、QLineEdit、QPushButton和QMessageBox等控件。程序中使用了布局管理器QVBoxLayout和QHBoxLayout进行控件的布局。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)