정보통신기술(ICT)

파이썬 GUI 모듈 인핸스(Enhanced) 타입

해머슴 2024. 11. 12. 10:29
import tkinter as tk
from tkinter import messagebox

# 주요 색상 설정
BG_COLOR = "#2C3E50"
BTN_COLOR = "#E74C3C"
HOVER_COLOR = "#C0392B"
TEXT_COLOR = "#ECF0F1"

# 버튼 호버 효과 함수
def on_enter(e):
    e.widget['background'] = HOVER_COLOR

def on_leave(e):
    e.widget['background'] = BTN_COLOR

# 계산기 기능
def calculate():
    try:
        expression = entry.get()
        result = eval(expression)
        entry.delete(0, tk.END)
        entry.insert(tk.END, str(result))
    except Exception:
        messagebox.showerror("Error", "Invalid Expression")

# GUI 초기화
root = tk.Tk()
root.title("Enhanced Calculator")
root.geometry("400x500")
root.config(bg=BG_COLOR)

# 입력 필드
entry = tk.Entry(root, font=("Arial", 24), bg=TEXT_COLOR, fg=BG_COLOR, borderwidth=0, relief="solid", justify="right")
entry.pack(pady=20, fill=tk.BOTH, padx=10)

# 버튼 생성 함수
def create_button(text, row, col, command=None):
    button = tk.Button(root, text=text, font=("Arial", 20), bg=BTN_COLOR, fg=TEXT_COLOR, relief="flat", command=command)
    button.grid(row=row, column=col, sticky="nsew", padx=5, pady=5)
    button.bind("<Enter>", on_enter)
    button.bind("<Leave>", on_leave)
    return button

# 버튼 배치
buttons = [
    ('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
    ('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
    ('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
    ('0', 4, 0), ('.', 4, 1), ('+', 4, 2), ('=', 4, 3),
]

# 버튼 배치 및 동작 추가
for (text, row, col) in buttons:
    if text == '=':
        create_button(text, row, col, calculate)
    else:
        create_button(text, row, col, lambda t=text: entry.insert(tk.END, t))

# 행, 열 설정으로 반응형 UI 구현
for i in range(5):
    root.grid_rowconfigure(i, weight=1)
    root.grid_columnconfigure(i, weight=1)

# GUI 실행
root.mainloop()

 

import tkinter as tk
from tkinter import messagebox

# 주요 색상 설정
BG_COLOR = "#2C3E50"
BTN_COLOR = "#E74C3C"
HOVER_COLOR = "#C0392B"
TEXT_COLOR = "#ECF0F1"

# 버튼 호버 효과 함수
def on_enter(e):
    e.widget['background'] = HOVER_COLOR

def on_leave(e):
    e.widget['background'] = BTN_COLOR

# 계산기 기능
def calculate():
    try:
        expression = entry.get()
        result = eval(expression)
        entry.delete(0, tk.END)
        entry.insert(tk.END, str(result))
    except Exception:
        messagebox.showerror("Error", "Invalid Expression")

# GUI 초기화
root = tk.Tk()
root.title("Enhanced Calculator")
root.geometry("400x500")
root.config(bg=BG_COLOR)

# 입력 필드
entry = tk.Entry(root, font=("Arial", 24), bg=TEXT_COLOR, fg=BG_COLOR, borderwidth=0, relief="solid", justify="right")
entry.pack(pady=20, fill=tk.BOTH, padx=10)

# 버튼 생성 함수
def create_button(text, row, col, command=None):
    button = tk.Button(root, text=text, font=("Arial", 20), bg=BTN_COLOR, fg=TEXT_COLOR, relief="flat", command=command)
    button.grid(row=row, column=col, sticky="nsew", padx=5, pady=5)
    button.bind("<Enter>", on_enter)
    button.bind("<Leave>", on_leave)
    return button

# 버튼 배치
buttons = [
    ('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
    ('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
    ('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
    ('0', 4, 0), ('.', 4, 1), ('+', 4, 2), ('=', 4, 3),
]

# 버튼 배치 및 동작 추가
for (text, row, col) in buttons:
    if text == '=':
        create_button(text, row, col, calculate)
    else:
        create_button(text, row, col, lambda t=text: entry.insert(tk.END, t))

# 행, 열 설정으로 반응형 UI 구현
for i in range(5):
    root.grid_rowconfigure(i, weight=1)
    root.grid_columnconfigure(i, weight=1)

# GUI 실행
root.mainloop()