赞
踩
本次的改进与前面两次变动很大,加入了登录与注册界面,检测跟踪界面也重新规划,界面进行了美化,加入了界面跳转功能等等。先上图
检测跟踪界面目前只实现打开摄像头这个功能,后续更新其他功能。
登录界面程序
from PyQt5.Qt import * from pyqt5_demo.resource.login_pane import Ui_Form import csv def user_message(): USER_PWD = {} with open('usermessage.csv', 'r') as csvfile: spamreader = csv.reader(csvfile) for row in spamreader: USER_PWD[row[0]] = row[1] return USER_PWD class LoginPane(QWidget, Ui_Form): show_register_pane_signal = pyqtSignal() show_login_in_signal = pyqtSignal() def __init__(self, parent=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.setAttribute(Qt.WA_StyledBackground, True) self.setupUi(self) def login(self): if self.username_ld.text() and self.password_ld.text(): self.login_in_btn.setEnabled(True) else: self.login_in_btn.setEnabled(False) def login_in(self): self.USER_PWD = user_message() if self.USER_PWD.get(self.username_ld.text()) == self.password_ld.text(): # QMessageBox.information(self, 'Information', 'Log in Successfully!') self.show_login_in_signal.emit() else: QMessageBox.critical(self, 'Wrong', '帐号密码错误!') self.username_ld.clear() self.password_ld.clear() def sign_in(self): self.show_register_pane_signal.emit() if __name__ == '__main__': import sys app = QApplication(sys.argv) window = LoginPane() window.show() sys.exit(app.exec_())
注册界面程序
from PyQt5.Qt import * from pyqt5_demo.resource.sign_pane import Ui_Form import csv class SignPane(QWidget, Ui_Form): exit_signal = pyqtSignal() check_login_signal = pyqtSignal(str, str) def __init__(self, parent=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.setAttribute(Qt.WA_StyledBackground, True) self.setupUi(self) self.USER_PWD = {} with open('usermessage.csv', 'r') as csvfile: spamreader = csv.reader(csvfile) for row in spamreader: self.USER_PWD[row[0]] = row[1] def sign_input_func(self): # 文本 if self.password_le.text() and self.username_le.text() and self.password_le1.text(): self.sign_in_btn.setEnabled(True) else: self.sign_in_btn.setEnabled(False) def sign_in_func(self): # 注册按键 if self.password_le.text() != self.password_le1.text(): QMessageBox.critical(self, 'Wrong', '两次输入密码不一样!') elif self.username_le.text() not in self.USER_PWD: self.USER_PWD[self.username_le.text()] = self.password_le.text() QMessageBox.information(self, 'Information', '注册成功!') self.check_login_signal.emit(self.username_le.text(), self.password_le.text()) else: QMessageBox.critical(self, 'Wrong', '该用户已存在!') self.password_le1.clear() self.password_le.clear() self.username_le.clear() def exit_func(self): self.exit_signal.emit() if __name__ == '__main__': import sys app = QApplication(sys.argv) window = SignPane() window.show() sys.exit(app.exec_())
检测与跟踪界面程序
from PyQt5.Qt import * from pyqt5_demo.resource.detect_and_track import Ui_Form import cv2 from PyQt5 import QtGui class DetectAndTrack(QWidget, Ui_Form): def __init__(self, parent=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.setAttribute(Qt.WA_StyledBackground, True) self.setupUi(self) self.timer = QTimer(self) self.timer.timeout.connect(self.TimerOutFun) self.camera = cv2.VideoCapture(0) def open_cam_func(self): self.timer.start(1) print("打开摄像头") def TimerOutFun(self): success, img = self.camera.read() if success: self.Image = img self.DispImg() def DispImg(self): self.img = cv2.cvtColor(self.Image, cv2.COLOR_BGR2RGB) showImage = QtGui.QImage(self.img.data, self.img.shape[1], self.img.shape[0], QtGui.QImage.Format_RGB888) self.label.setPixmap(QtGui.QPixmap.fromImage(showImage)) if __name__ == '__main__': import sys app = QApplication(sys.argv) window = DetectAndTrack() window.show() sys.exit(app.exec_())
主程序
from pyqt5_demo.login_in_pane import LoginPane from pyqt5_demo.sign_in_pane import SignPane from PyQt5.Qt import * from pyqt5_demo.detect_track_demo import DetectAndTrack import csv if __name__ == '__main__': import sys app = QApplication(sys.argv) # 控件面板的创建 login_pane = LoginPane() register_pane = SignPane(login_pane) detect_track_pane = DetectAndTrack() register_pane.move(0, login_pane.height()) register_pane.show() # 槽函数 def exit_register_pane(): animation = QPropertyAnimation(register_pane) animation.setTargetObject(register_pane) animation.setPropertyName(b"pos") animation.setStartValue(QPoint(0, 0)) animation.setEndValue(QPoint(login_pane.width(), 0)) animation.setDuration(300) animation.setEasingCurve(QEasingCurve.InBounce) animation.start(QAbstractAnimation.DeleteWhenStopped) def show_register_pane(): animation = QPropertyAnimation(register_pane) animation.setTargetObject(register_pane) animation.setPropertyName(b"pos") animation.setStartValue(QPoint(0, login_pane.height())) animation.setEndValue(QPoint(0, 0)) animation.setDuration(300) animation.setEasingCurve(QEasingCurve.OutBounce) animation.start(QAbstractAnimation.DeleteWhenStopped) def save_message(account, pwd): headers = ['username', 'password'] values = [{'username': account, 'password': pwd}] with open('usermessage.csv', 'a', encoding='utf-8', newline='') as fp: writer = csv.DictWriter(fp, headers) # 写入表头数据的时候,需要调用writeheader方法 # writer.writeheader() writer.writerows(values) def show_detct_track(): print("a") detect_track_pane.show() login_pane.close() # 信号的链接 login_pane.show_register_pane_signal.connect(show_register_pane) login_pane.show_login_in_signal.connect(show_detct_track) register_pane.exit_signal.connect(exit_register_pane) register_pane.check_login_signal.connect(save_message) login_pane.show() sys.exit(app.exec_())
每个界面的程序都可以单独运行。主程序可以完整运行所有界面,并实现界面跳转。界面用qtdesigner设计,生成的py文件就不上传。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。