[Python] tkinter 를 이용한 로그인 화면 만들기
from tkinter import *
from tkinter import ttk
# tkinter 객체 생성
window = Tk()
window.title("Login Screen")
# 사용자 id, password, email, phone을 저장하는 변수 생성
user_id, password, email, phone = StringVar(), StringVar(), StringVar(), StringVar()
# 사용자 id와 password를 비교하는 함수
def check_data():
if user_id.get() == "Passing" and password.get() == "Story":
status_label.config(text="You are logged in successfully.", foreground="green")
else:
status_label.config(text="Check your Username/Password.", foreground="red")
# UI 구성
ttk.Label(window, text="Username : ").grid(row=0, column=0, padx=10, pady=10)
ttk.Entry(window, textvariable=user_id).grid(row=0, column=1, padx=10, pady=10)
ttk.Label(window, text="Password : ").grid(row=1, column=0, padx=10, pady=10)
ttk.Entry(window, textvariable=password, show="*").grid(row=1, column=1, padx=10, pady=10)
ttk.Label(window, text="Email : ").grid(row=2, column=0, padx=10, pady=10)
ttk.Entry(window, textvariable=email).grid(row=2, column=1, padx=10, pady=10)
ttk.Label(window, text="Phone : ").grid(row=3, column=0, padx=10, pady=10)
ttk.Entry(window, textvariable=phone).grid(row=3, column=1, padx=10, pady=10)
ttk.Button(window, text="Login", command=check_data).grid(row=4, column=1, padx=10, pady=10)
# 로그인 상태를 표시하는 라벨
status_label = ttk.Label(window, text="", foreground="red")
status_label.grid(row=5, column=0, columnspan=2, pady=10)
window.mainloop()
Key Changes and Additions:
- Added Fields for Email and Phone:
- Two additional StringVar() variables: email and phone.
- Corresponding labels and entry widgets for Email and Phone.
- Improved Feedback:
- A label (status_label) is added to display login status.
- Feedback is shown in green if login is successful and in red if it fails.
- Password Entry:
- Added show="*" to the password entry field to hide the input.
This code creates a user-friendly login interface with the required fields and dynamic feedback upon login attempts.
주요 변경 및 추가 사항:
이메일 및 전화 필드가 추가되었습니다:
이메일과 전화라는 두 가지 StringVar() 변수가 추가되었습니다.
이메일과 전화에 해당하는 레이블과 입력 위젯이 추가되었습니다.
피드백 개선:
로그인 상태를 표시하는 레이블(status_label)이 추가되었습니다.
로그인에 성공하면 녹색으로, 실패하면 빨간색으로 피드백이 표시됩니다.
비밀번호 입력:
비밀번호 입력 필드에 show=“*”를 추가하여 입력을 숨겼습니다.
이 코드는 필수 필드와 로그인 시도 시 동적 피드백이 포함된 사용자 친화적인 로그인 인터페이스를 만듭니다.