pyqt5 mysql登录注册系统界面
时间: 2023-10-19 09:05:19 浏览: 24
好的,您需要一个PyQt5和MySQL的环境以及相关的库。以下是一个简单的登录注册系统的界面示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout
import mysql.connector
class Login(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Login')
self.resize(300, 150)
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.register_button = QPushButton('Register')
vbox = QVBoxLayout()
hbox1 = QHBoxLayout()
hbox2 = QHBoxLayout()
hbox1.addWidget(self.username_label)
hbox1.addWidget(self.username_edit)
hbox2.addWidget(self.password_label)
hbox2.addWidget(self.password_edit)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addWidget(self.login_button)
vbox.addWidget(self.register_button)
self.setLayout(vbox)
self.login_button.clicked.connect(self.login)
self.register_button.clicked.connect(self.register)
def login(self):
username = self.username_edit.text()
password = self.password_edit.text()
db = mysql.connector.connect(
host='localhost',
user='root',
passwd='password',
database='test'
)
cursor = db.cursor()
cursor.execute('SELECT * FROM users WHERE username=%s AND password=%s', (username, password))
result = cursor.fetchone()
if result:
print('Login successful')
else:
print('Login failed')
def register(self):
pass
class Register(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Register')
self.resize(300, 150)
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.confirm_password_label = QLabel('Confirm Password:')
self.confirm_password_edit = QLineEdit()
self.confirm_password_edit.setEchoMode(QLineEdit.Password)
self.register_button = QPushButton('Register')
vbox = QVBoxLayout()
hbox1 = QHBoxLayout()
hbox2 = QHBoxLayout()
hbox3 = QHBoxLayout()
hbox1.addWidget(self.username_label)
hbox1.addWidget(self.username_edit)
hbox2.addWidget(self.password_label)
hbox2.addWidget(self.password_edit)
hbox3.addWidget(self.confirm_password_label)
hbox3.addWidget(self.confirm_password_edit)
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addLayout(hbox3)
vbox.addWidget(self.register_button)
self.setLayout(vbox)
self.register_button.clicked.connect(self.register)
def register(self):
username = self.username_edit.text()
password = self.password_edit.text()
confirm_password = self.confirm_password_edit.text()
if password != confirm_password:
print('Passwords do not match')
return
db = mysql.connector.connect(
host='localhost',
user='root',
passwd='password',
database='test'
)
cursor = db.cursor()
cursor.execute('INSERT INTO users (username, password) VALUES (%s, %s)', (username, password))
db.commit()
print('Registration successful')
if __name__ == '__main__':
app = QApplication(sys.argv)
login = Login()
register = Register()
login.show()
sys.exit(app.exec_())
```
这个例子包含了一个简单的登录和注册界面,使用了PyQt5和MySQL。您可以根据自己的需要进行修改和扩展。