PyQt5设计一个登录界面,包含注册和登录功能。并连接到数据库。第二个界面是YOLOv4模型的检测效果可视化界面
时间: 2024-03-07 20:52:35 浏览: 20
下面是一个简单的PyQt5代码示例,演示如何设计一个登录界面并连接到数据库,并且包含注册和登录功能。同时,我们还设计了一个第二个界面,用于显示YOLOv4模型的检测效果可视化。
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QLabel, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QFont, QPixmap, QImage
from PyQt5.QtCore import Qt
import sqlite3
import cv2
class LoginWidget(QWidget):
def __init__(self):
super().__init__()
# 设置窗口标题和大小
self.setWindowTitle("登录界面")
self.setFixedSize(400, 300)
# 设置字体
font = QFont()
font.setFamily("Microsoft YaHei")
font.setPointSize(14)
# 创建用户名和密码框
self.username_edit = QLineEdit()
self.username_edit.setPlaceholderText("请输入用户名")
self.username_edit.setFont(font)
self.password_edit = QLineEdit()
self.password_edit.setPlaceholderText("请输入密码")
self.password_edit.setEchoMode(QLineEdit.Password)
self.password_edit.setFont(font)
# 创建登录和注册按钮
login_button = QPushButton("登录")
login_button.setFont(font)
login_button.setStyleSheet("background-color: #007ACC; color: white; border-radius: 5px;")
login_button.clicked.connect(self.login)
register_button = QPushButton("注册")
register_button.setFont(font)
register_button.setStyleSheet("background-color: #007ACC; color: white; border-radius: 5px;")
register_button.clicked.connect(self.register)
# 创建标签
title_label = QLabel("欢迎使用登录界面")
title_label.setAlignment(Qt.AlignCenter)
title_label.setFont(font)
subtitle_label = QLabel("请先登录或注册")
subtitle_label.setAlignment(Qt.AlignCenter)
subtitle_label.setFont(font)
# 创建布局
vbox = QVBoxLayout()
vbox.addWidget(title_label)
vbox.addWidget(subtitle_label)
vbox.addWidget(self.username_edit)
vbox.addWidget(self.password_edit)
hbox = QHBoxLayout()
hbox.addWidget(login_button)
hbox.addWidget(register_button)
vbox.addLayout(hbox)
self.setLayout(vbox)
# 连接数据库
self.conn = sqlite3.connect("users.db")
self.cursor = self.conn.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS users (username TEXT, password TEXT)")
self.conn.commit()
def __del__(self):
# 关闭数据库连接
self.conn.close()
def login(self):
username = self.username_edit.text()
password = self.password_edit.text()
# 查询用户是否存在
self.cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
if len(self.cursor.fetchall()) == 0:
# 用户不存在
QMessageBox.warning(self, "登录失败", "用户名或密码错误!")
else:
# 登录成功,打开第二个界面
self.close()
self.second_widget = SecondWidget()
self.second_widget.show()
def register(self):
username = self.username_edit.text()
password = self.password_edit.text()
# 查询用户是否存在
self.cursor.execute("SELECT * FROM users WHERE username=?", (username,))
if len(self.cursor.fetchall()) > 0:
# 用户已存在
QMessageBox.warning(self, "注册失败", "用户名已存在!")
else:
# 添加用户
self.cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
self.conn.commit()
QMessageBox.information(self, "注册成功", "注册成功!")
class SecondWidget(QWidget):
def __init__(self):
super().__init__()
# 设置窗口标题和大小
self.setWindowTitle("YOLOv4检测效果可视化界面")
self.setFixedSize(800, 600)
# 加载图片
self.image = cv2.imread("test.jpg")
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
self.qimage = QImage(self.image.data, self.image.shape[1], self.image.shape[0], QImage.Format_RGB888)
# 创建标签
label = QLabel()
label.setPixmap(QPixmap.fromImage(self.qimage))
# 创建布局
vbox = QVBoxLayout()
vbox.addWidget(label)
self.setLayout(vbox)
if __name__ == "__main__":
app = QApplication([])
login_widget = LoginWidget()
login_widget.show()
app.exec_()
```
在这个例子中,我们使用了sqlite3作为数据库,并创建了一个users表,用于存储用户信息。我们设计了一个登录界面,包含用户名和密码框、登录和注册按钮,以及相应的功能。当用户登录成功后,我们打开第二个界面,用于显示YOLOv4模型的检测效果可视化。在第二个界面中,我们使用了OpenCV和PyQt5来加载和显示一张图片。
在实际应用中,您可以根据需要自定义更多的组件和功能,以满足您的需求。