1. 시험환경
· python
· PyQt5
2. 목적
· PyQt를 이용한 파이썬 GUI 프로그램을 만들어보자.
· PyQt를 이용한 로그인 화면을 구성하고, 로그인 성공 시 메인화면으로 전환하는 방법을 알아보자.
3. 적용
① 로그인 화면 윈도우
- login_window.py
- ID: test, PW: password 입력시, 임포트한 MainWindow가 호출되면서 화면 전환되는 부분을 확인한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# login_window.py
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox, QApplication
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("로그인")
self.setGeometry(100, 100, 300, 150)
self.main_window = None # MainWindow 객체를 저장할 변수
self.initUI()
def initUI(self):
self.id_label = QLabel("아이디:")
self.id_input = QLineEdit()
self.pw_label = QLabel("비밀번호:")
self.pw_input = QLineEdit()
self.pw_input.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton("로그인")
self.login_button.clicked.connect(self.login_clicked)
layout = QVBoxLayout()
layout.addWidget(self.id_label)
layout.addWidget(self.id_input)
layout.addWidget(self.pw_label)
layout.addWidget(self.pw_input)
layout.addWidget(self.login_button)
layout.addStretch(1)
self.setLayout(layout)
def login_clicked(self):
user_id = self.id_input.text()
password = self.pw_input.text()
# 간단한 로그인 검증 (실제 환경에서는 DB 연동 필요)
if user_id == "test" and password == "password":
QMessageBox.information(self, "로그인 성공", "로그인에 성공했습니다.")
self.open_main_window()
else:
QMessageBox.warning(self, "로그인 실패", "아이디 또는 비밀번호가 잘못되었습니다.")
def open_main_window(self):
from main_window import MainWindow # import here to avoid circular import
self.main_window = MainWindow(self) # Pass LoginWindow instance to MainWindow
self.main_window.show()
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
login_window = LoginWindow()
login_window.show()
sys.exit(app.exec_())
|
cs |
② 메인 화면 윈도우
- main_window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# main_window.py
from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout, QMessageBox
class MainWindow(QWidget):
def __init__(self, login_window):
super().__init__()
self.setWindowTitle("메인 화면")
self.setGeometry(100, 100, 400, 300)
self.login_window = login_window # Store the LoginWindow instance
self.initUI()
def initUI(self):
self.welcome_label = QLabel("메인 화면에 오신 것을 환영합니다!")
self.logout_button = QPushButton("로그아웃")
self.logout_button.clicked.connect(self.logout_clicked)
layout = QVBoxLayout()
layout.addWidget(self.welcome_label)
layout.addWidget(self.logout_button)
layout.addStretch(1)
self.setLayout(layout)
def logout_clicked(self):
reply = QMessageBox.question(self, '로그아웃', '로그아웃 하시겠습니까?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.login_window.show() # Show the LoginWindow again
self.close()
if __name__ == '__main__':
# This part will not be executed when main_window.py is imported
pass
|
cs |
4. 결과
· 프로그램 실행
- python ./login_window.py
· 로그인 정보 입력
- ID: test, PW: password
· 프로그램 다운로드
'파이썬' 카테고리의 다른 글
파이썬 가상환경(venv) 설정 및 사용법 (1) | 2024.12.15 |
---|---|
파이썬을 이용한 wordpress(워드프레스) 자동 포스팅 (0) | 2024.11.26 |
유튜브 자막추출 파이썬 코드 (feat. yotube_transcript_api 라이브러리) (5) | 2024.09.07 |
텍스트 음성변환(SST; SpeechToText) 파이썬 코드 (1) | 2024.09.07 |
numpy 사용법 기초 (2/2) (6) | 2024.09.07 |