diff --git a/login.py b/login.py new file mode 100644 index 0000000..41e876d --- /dev/null +++ b/login.py @@ -0,0 +1,79 @@ +import mysql.connector as mc +from PyQt5.QtWidgets import * +import sys +from menu import MainMenu + +# MySQL 연결 설정 +def connect_to_mysql(): + mydb = mc.connect( + host="34.22.65.53", + user="root", + password="cms", + database="cms" + ) + return mydb + +class LoginWindow(QWidget): + def __init__(self): + super().__init__() + self.title = "Login" + self.initUI() + self.main_menu = None # MainMenu 인스턴스 + + def initUI(self): + self.setWindowTitle(self.title) + + self.label_email = QLabel("Email:", self) + self.lineEdit_email = QLineEdit(self) + + self.label_password = QLabel("Password:", self) + self.lineEdit_password = QLineEdit(self) + self.lineEdit_password.setEchoMode(QLineEdit.Password) + + self.button_login = QPushButton("Login", self) + self.button_login.clicked.connect(self.login) + + layout = QVBoxLayout() + layout.addWidget(self.label_email) + layout.addWidget(self.lineEdit_email) + layout.addWidget(self.label_password) + layout.addWidget(self.lineEdit_password) + layout.addWidget(self.button_login) + + self.setLayout(layout) + + def login(self): + email = self.lineEdit_email.text() + password = self.lineEdit_password.text() + + # MySQL 연결 + mydb = connect_to_mysql() + mycursor = mydb.cursor() + + # id와 password로 인증 확인 + query = "SELECT id FROM teacher WHERE id = %s AND password = %s" + values = (email, password) + mycursor.execute(query, values) + result = mycursor.fetchone() + + if result is not None: + self.hide() + self.main_menu = MainMenu() # MainMenu 인스턴스 생성 + self.main_menu.show() + else: + QMessageBox.warning(self, "Login Failed", "Incorrect email or password") + + # 연결 종료 + mycursor.close() + mydb.close() + + def closeEvent(self, event): + if self.main_menu is not None: + self.main_menu.show() # 메인 메뉴 창 보여주기 + event.accept() + +if __name__ == '__main__': + app = QApplication(sys.argv) + mainWindow = LoginWindow() + mainWindow.show() + sys.exit(app.exec_()) \ No newline at end of file diff --git a/menu.py b/menu.py index 8c8da46..b27b8bc 100644 --- a/menu.py +++ b/menu.py @@ -1,6 +1,7 @@ -from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton +from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel from register import Register from attendance import Attendance +import sys class MainMenu(QWidget): def __init__(self): @@ -33,7 +34,8 @@ def open_attendance(self): self.attendance = Attendance(self) self.attendance.show() -app = QApplication([]) -ex = MainMenu() -ex.show() -app.exec_() \ No newline at end of file +if __name__ == "__main__": + app = QApplication([]) + ex = MainMenu() + ex.show() + app.exec_() \ No newline at end of file