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()
'정보통신기술(ICT)' 카테고리의 다른 글
한국어-영어 맞춤법 기초 검사기 (2) | 2024.11.14 |
---|---|
파이썬 코드로 한국이 가진 오방색을 나타내는 방식 (0) | 2024.11.13 |
파이썬-기반 GUI 볼텍스 형상화(Python-based GUI Vortex Visualization) (0) | 2024.11.11 |
초연공학(Conscious Engineering) (0) | 2024.11.10 |
초전도체 연구에 필요한 양자물리학 기반 뉴트론시스 방정식(Neutronsys Equation) (3) | 2024.11.07 |