pyqt5 login
时间: 2023-12-16 09:26:47 浏览: 124
pyqt5实现的登录功能
4星 · 用户满意度95%
以下是一个简单的PyQt5登录界面的例子,其中用户名和密码存储在ini文件中:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from configparser import ConfigParser
class Login(QWidget):
def __init__(self):
super().__init__()
self.title = 'Login'
self.left = 10
self.top = 10
self.width = 400
self.height = 140
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowIcon(QIcon('icon.png'))
self.label_username = QLabel('Username:', self)
self.label_username.move(20, 20)
self.textbox_username = QLineEdit(self)
self.textbox_username.move(100, 20)
self.textbox_username.resize(280, 20)
self.label_password = QLabel('Password:', self)
self.label_password.move(20, 50)
self.textbox_password = QLineEdit(self)
self.textbox_password.setEchoMode(QLineEdit.Password)
self.textbox_password.move(100, 50)
self.textbox_password.resize(280, 20)
self.button_login = QPushButton('Login', self)
self.button_login.move(20, 90)
self.button_login.clicked.connect(self.login)
self.button_cancel = QPushButton('Cancel', self)
self.button_cancel.move(300, 90)
self.button_cancel.clicked.connect(self.close)
self.show()
def login(self):
username = self.textbox_username.text()
password = self.textbox_password.text()
config = ConfigParser()
config.read('users.ini')
if username in config.sections():
if password == config[username]['password']:
QMessageBox.information(self, 'Login', 'Login successful!')
self.close()
# 在这里添加跳转到其他界面的代码
else:
QMessageBox.warning(self, 'Login', 'Incorrect password!')
else:
QMessageBox.warning(self, 'Login', 'User not found!')
if __name__ == '__main__':
app = QApplication(sys.argv)
login = Login()
sys.exit(app.exec_())
```
阅读全文