# import the required modules for the GUI application. from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QLabel import sys import time import serial import adafruit_fingerprint # If using with a computer such as Linux/RaspberryPi, Mac, Windows with USB/serial converter: uart = serial.Serial("/dev/ttyUSB0", baudrate=57600, timeout=1) finger = adafruit_fingerprint.Adafruit_Fingerprint(uart) # Define a class that will contain the UI elements and their behavior class Ui_MainWindow(object): # This function sets up the UI elements def setupUi(self, MainWindow): # Set the window title and size MainWindow.setObjectName("Fingerprint Reader") MainWindow.resize(480, 320) MainWindow.setMinimumSize(QtCore.QSize(480, 320)) MainWindow.setMaximumSize(QtCore.QSize(480, 320)) # Set the background color of the window and the text color MainWindow.setStyleSheet("background-color: rgb(5,35,50);") # Set the shape of the tab widget MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded) # Create a central widget to hold other widgets self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") # Create a label widget to display an image self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(-20, 0, 501, 311)) self.label.setStyleSheet("color: rgb(9, 32, 44);") self.label.setText("") self.label.setPixmap(QtGui.QPixmap("image.png")) self.label.setObjectName("label") # Create a push button widget to show a menu self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(10, 10, 71, 31)) self.pushButton.setStyleSheet("color: rgb(255, 255, 255);") icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("icons8-menu-90.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.pushButton.setIcon(icon) self.pushButton.setObjectName("pushButton") # Create a widget to display the time self.timeWidget = QtWidgets.QWidget(self.centralwidget) self.timeWidget.setGeometry(QtCore.QRect(-10, 60, 171, 181)) self.timeWidget.setObjectName("timeWidget") # Create a Digital Clock inside the timeWidget self.lcdNumber = QtWidgets.QLCDNumber(self.timeWidget) self.lcdNumber.setGeometry(QtCore.QRect(0, 60, 171, 61)) self.lcdNumber.setStyleSheet("color: rgb(255, 255, 255);\nborder: none;") self.lcdNumber.setObjectName("lcdNumber") # Create a second digital clock self.lcdNumber_2 = QtWidgets.QLCDNumber(self.timeWidget) self.lcdNumber_2.setGeometry(QtCore.QRect(50, 125, 121, 41)) self.lcdNumber_2.setStyleSheet("color: rgb(255, 255, 255);\nborder: none;") self.lcdNumber_2.setObjectName("lcdNumber_2") # Set the LCD widget to display hour, minute, and second self.timer = QtCore.QTimer(self.timeWidget) self.timer.timeout.connect(self.updateLCD) self.timer.start(1000) # Create a widget to display the date self.dateWidget = QtWidgets.QWidget(self.centralwidget) self.dateWidget.setGeometry(QtCore.QRect(150, 10, 201, 31)) self.dateWidget.setStyleSheet("background-color: rgb(9, 32 44);") self.dateWidget.setObjectName("dateWidget") # Create a label widget to display the date self.dateLabel = QtWidgets.QLabel(self.dateWidget) self.dateLabel.setGeometry(QtCore.QRect(0, 0, 201, 31)) font = QtGui.QFont() font.setPointSize(10) font.setBold(True) font.setWeight(75) self.dateLabel.setFont(font) self.dateLabel.setStyleSheet("color: rgb(255, 255, 255);") self.dateLabel.setAlignment(QtCore.Qt.AlignCenter) self.dateLabel.setObjectName("dateLabel") # Set the LCD widget to display hour, minute, second, weekday, day, month, and year self.timer = QtCore.QTimer(self.dateWidget) self.timer.timeout.connect(self.updateDate) self.timer.start(1000) # Set the central widget of the main window MainWindow.setCentralWidget(self.centralwidget) # Create a status bar widget self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") # Set the status bar of the main window MainWindow.setStatusBar(self.statusbar) # Connect the UI elements to their respective functions self.retranslateUi(MainWindow) self.pushButton.clicked.connect(self.showMenu) QtCore.QMetaObject.connectSlotsByName(MainWindow) # This function sets the text for UI elements that require ita def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("Fingerprint Reader", "Fingerprint Reader")) self.pushButton.setText(_translate("Fingerprint Reader", "Menu")) def updateLCD(self): # Get the current time as a formatted string time_str = time.strftime('%H:%M') time_str_2 = time.strftime('%S') # Update the LCD widget with the time string self.lcdNumber.display(time_str) self.lcdNumber_2.display(time_str_2) def updateDate(self): # Get the current date and time current_datetime = QtCore.QDateTime.currentDateTime() # Get the formatted strings for the date and time weekday_str = current_datetime.toString("dddd") day_str = current_datetime.toString("dd") month_str = current_datetime.toString("MMM") year_str = current_datetime.toString("yyyy") # Combine the date and time strings datetime_str = f"{weekday_str} {day_str}/{month_str}/{year_str}" # Update the label widget with the date and time string self.dateLabel.setText(datetime_str) def showMessage(self, messageBox, text): messageBox.setText(text) messageBox.show() # This function displays a menu with two options def showMenu(self): # Create a message box with two buttons msgBox = QtWidgets.QMessageBox() msgBox.setStyleSheet("background-color: rgb(9, 32 44);") # Remove the window border and the title bar msgBox.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint) msgBox.setWindowTitle("Menu") msgBox.setText("Please select an option:") # Get the QLabel widget inside the message box label = msgBox.findChild(QtWidgets.QLabel) # Set the alignment of the label to center label.setAlignment(QtCore.Qt.AlignCenter) # Add Buttons and their styles addworker_style = ''' QPushButton { background-color: #1D8CC3; border: none; color: white; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } QPushButton:pressed { background-color: #13597c; } ''' leave_style = ''' QPushButton { background-color: #cf2f27; border: none; color: white; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } QPushButton:pressed { background-color: #89201A; } ''' msgBox.addButton("Add worker", QtWidgets.QMessageBox.AcceptRole).setStyleSheet(addworker_style) msgBox.addButton(" Leave ", QtWidgets.QMessageBox.RejectRole).setStyleSheet(leave_style) # Add styles to the menu msgBox.setStyleSheet("QMessageBox { background-color: rgb(8, 52, 73); border: 1px solid black; }" "QMessageBox QLabel { color: white; font-size: 20px;}") # Create a timer to close the message box after 7.5 seconds timer = QtCore.QTimer() timer.setSingleShot(True) timer.timeout.connect(msgBox.accept) timer.start(7500) # Show the message box and handle the result result = msgBox.exec_() if result == QtWidgets.QMessageBox.AcceptRole: print("Add worker selected") messageBox = QtWidgets.QMessageBox() messageBox.setStandardButtons(QtWidgets.QMessageBox.NoButton) messageBox.setStyleSheet("background-color: #09202C; color: rgb(255, 255, 255); font-weight: bold; text-align: center;") self.showMessage(messageBox, "Wait") #Take a 2 finger images and template it, then store in 'location for fingerimg in range(1, 3): if fingerimg == 1: messageBox.close() self.showMessage(messageBox, "Place finger on sensor...") else: messageBox.close() self.showMessage(messageBox, "Place same finger again...") while True: i = finger.get_image() if i == adafruit_fingerprint.OK: messageBox.close() self.showMessage(messageBox, "Image taken") break if i == adafruit_fingerprint.NOFINGER: print(".", end="") elif i == adafruit_fingerprint.IMAGEFAIL: print("Imaging error") return False else: print("Other error") return False messageBox.close() self.showMessage(messageBox, "Templating...") i = finger.image_2_tz(fingerimg) if i == adafruit_fingerprint.OK: messageBox.close() self.showMessage(messageBox, "Templated") else: if i == adafruit_fingerprint.IMAGEMESS: print("Image too messy") elif i == adafruit_fingerprint.FEATUREFAIL: print("Could not identify features") elif i == adafruit_fingerprint.INVALIDIMAGE: print("Image invalid") else: print("Other error") return False if fingerimg == 1: messageBox.close() self.showMessage(messageBox, "Remove finger") time.sleep(1) while i != adafruit_fingerprint.NOFINGER: i = finger.get_image() messageBox.close() self.showMessage(messageBox, "Creating model...") i = finger.create_model() if i == adafruit_fingerprint.OK: messageBox.close() self.showMessage(messageBox, "Created") else: if i == adafruit_fingerprint.ENROLLMISMATCH: print("Prints did not match") else: print("Other error") return False messageBox.close() self.showMessage(messageBox, "Storing model...") i = finger.store_model(0) if i == adafruit_fingerprint.OK: messageBox.close() self.showMessage(messageBox, "Stored") else: if i == adafruit_fingerprint.BADLOCATION: print("Bad storage location") elif i == adafruit_fingerprint.FLASHERR: print("Flash storage error") else: print("Other error") return False return True # If this script is run as the main program, create the application and window if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() # Create the UI object and set up the UI ui = Ui_MainWindow() ui.setupUi(MainWindow) # Show the main window MainWindow.show() # Run the application event loop sys.exit(app.exec_())