반응형
from tkinter import *
import openpyxl
import datetime
# 엑셀 파일이 존재하지 않으면 생성하는 함수
def create_excel_file():
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['Task', 'Customer', 'Time'])
wb.save('customer_response.xlsx')
# 엑셀 파일이 존재하는지 확인하고 없으면 생성하는 부분
try:
wb = openpyxl.load_workbook('customer_response.xlsx')
ws = wb.active
except:
create_excel_file()
wb = openpyxl.load_workbook('customer_response.xlsx')
ws = wb.active
# 버튼을 클릭했을 때 실행되는 함수
def button_clicked(task_num):
# 고객 이름을 입력받는 창 생성
name_window = Toplevel(root)
name_label = Label(name_window, text='Enter customer name:')
name_label.pack(side=LEFT)
name_entry = Entry(name_window)
name_entry.pack(side=LEFT)
name_entry.focus_set()
# 엔터 키를 누르면 엑셀 파일에 작성
def enter_pressed(event):
name = name_entry.get()
row = (ws.max_row)+1
ws.cell(row=row, column=1, value=task_num)
ws.cell(row=row, column=2, value=name)
ws.cell(row=row, column=3, value=datetime.datetime.now())
wb.save('customer_response.xlsx')
name_window.destroy()
name_entry.bind('<Return>', enter_pressed)
# tkinter GUI 생성
root = Tk()
# 각 버튼 클릭 시 실행할 함수 설정
button1 = Button(root, text="Task 1", command=lambda: button_clicked(1))
button2 = Button(root, text="Task 2", command=lambda: button_clicked(2))
button3 = Button(root, text="Task 3", command=lambda: button_clicked(3))
button4 = Button(root, text="Task 4", command=lambda: button_clicked(4))
button5 = Button(root, text="Task 5", command=lambda: button_clicked(5))
button6 = Button(root, text="Task 6", command=lambda: button_clicked(6))
button7 = Button(root, text="Task 7", command=lambda: button_clicked(7))
button8 = Button(root, text="Task 8", command=lambda: button_clicked(8))
# 버튼 위치 설정
button1.grid(row=0, column=0, padx=5, pady=5, sticky='nesw')
button2.grid(row=0, column=1, padx=5, pady=5, sticky='nesw')
button3.grid(row=0, column=2, padx=5, pady=5, sticky='nesw')
button4.grid(row=1, column=0, padx=5, pady=5, sticky='nesw')
button5.grid(row=1, column=1, padx=5, pady=5, sticky='nesw')
button6.grid(row=1, column=2, padx=5, pady=5, sticky='nesw')
button7.grid(row=2, column=0, padx=5, pady=5, sticky='nesw')
button8.grid(row=2, column=1, padx=5, pady=5, sticky='nesw')
# GUI 실행
root.mainloop()
간단한건 완벽한데, 조금만 기능이 추가되면.. 중간에 답변 끊어먹고 코드 이상하게 수정해주고.. 으휴
from tkinter import *
from datetime import datetime
import openpyxl
import os
root = Tk()
root.title("고객 대응 현황")
root.geometry("400x400")
filename = 'customer_info.xlsx'
# 엑셀 파일 생성
if not os.path.exists(filename):
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(['Task', 'Customer', 'Time'])
wb.save(filename)
# 각 task별 count 초기값
counts = {"task1": 0, "task2": 0, "task3": 0, "task4": 0,
"task5": 0, "task6": 0, "task7": 0, "task8": 0}
# 고객 대응 시간 함수
def get_time():
now = datetime.now()
return now.strftime("%Y-%m-%d %H:%M:%S")
# 엑셀 파일에 기록하는 함수
def write_to_excel(task, customer):
wb = openpyxl.load_workbook(filename)
sheet = wb.active
sheet.append([task, customer, get_time()])
wb.save(filename)
# 각 task별로 업데이트된 count 계산
def update_counts():
global counts
wb = openpyxl.load_workbook(filename)
sheet = wb.active
for row in sheet.iter_rows(min_row=2, values_only=True):
task = row[0]
if task in counts:
counts[task] += 1
wb.close()
# 각 task 버튼 아래에 count 업데이트
def update_buttons():
global counts
for i, button in enumerate(task_buttons):
count_label = Label(root, text=str(counts[button["text"]]))
count_label.grid(row=i+2, column=1)
# 고객 입력 창
def input_customer(task):
customer_input = Toplevel()
customer_input.geometry("200x100")
customer_input.title(task)
customer_label = Label(customer_input, text="고객 이름:")
customer_label.pack(pady=10)
customer_entry = Entry(customer_input, width=30)
customer_entry.pack()
def submit():
customer = customer_entry.get()
if customer:
write_to_excel(task, customer)
update_counts()
update_buttons()
customer_entry.delete(0, END)
confirm = Toplevel()
confirm.geometry("150x50")
confirm_label = Label(confirm, text="입력되었습니다.")
confirm_label.pack(pady=10)
confirm_button = Button(confirm, text="확인", command=confirm.destroy)
confirm_button.pack()
customer_input.destroy()
customer_entry.bind('<Return>', lambda event: submit())
submit_button = Button(customer_input, text="Submit", command=submit)
# 업무별 개수 count 함수
def count_task():
count = [0] * len(counts)
for i in range(len(data)):
task = data[i][0]
count[tasks.index(task)] += 1
for i in range(len(tasks)):
task_labels[i].config(text=tasks[i] + " (" + str(count[i]) + ")")
# task 버튼 클릭시 실행 함수
def task_clicked(task):
# 고객 이름을 입력받는 창 생성
name_window = Toplevel(root)
name_label = Label(name_window, text='Enter customer name:')
name_label.pack(side=LEFT)
name_entry = Entry(name_window)
name_entry.pack(side=LEFT)
name_entry.focus_set()
def enter_pressed(event):
customer = name_entry.get()
if customer != "":
# 현재 시간과 입력받은 고객 이름을 데이터에 추가
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 데이터를 엑셀 파일에 저장
wb = openpyxl.load_workbook(filename)
ws = wb.active
ws.append([task, customer, current_time])
wb.save(filename)
# 고객 입력창 삭제
name_entry.destroy()
# 입력되었다는 문구 출력
name_label.config(text="Recorded: " + task + " - " + customer)
# 업무별 개수 count
count_task()
# 화면을 업무 버튼들이 있는 task_frame으로 전환
root.tkraise()
# 엔터키 누르면 고객 이름 입력 완료
name_entry.bind('<Return>', enter_pressed)
# task 버튼 생성
task_buttons = []
for i, task in enumerate(counts):
task_button = Button(root, text=task, width=15, height=2, font=("Arial", 12),
command=lambda t=task: task_clicked(t))
task_button.grid(row=i//2, column=i%2, padx=10, pady=10)
task_buttons.append(task_button)
반응형