'메모장코딩'에 해당되는 글 1건

  1. 2023.02.28 파이썬으로 메모장 만들기

소개

이번 포스트에서는 파이썬으로 간단한 메모장을 만들어보겠습니다. 이 프로그램은 기본적인 메모장의 기능을 제공하며, 사용자는 메모를 작성하고 저장할 수 있습니다.

프로그램 설계

메모장을 만들기 위해서는 사용자로부터 명령을 받아들이고, 그 명령을 처리하는 기능이 필요합니다. 이를 위해 파이썬의 표준 라이브러리인 tkinter 모듈을 사용하여 GUI를 구성하고, 파일 입출력을 위한 기능을 추가합니다.

tkinter 모듈

tkinter 모듈은 파이썬에서 GUI(Graphical User Interface)를 구현하기 위한 표준 라이브러리입니다. 이 모듈을 사용하여 윈도우 창과 버튼, 레이블 등을 만들어 GUI를 구성할 수 있습니다. tkinter 모듈은 파이썬 3 이상부터 기본 모듈로 설치되어 있으므로 별도의 설치가 필요하지 않습니다.

파일 입출력

파일 입출력은 메모장에서 가장 중요한 기능 중 하나입니다. 이 기능을 구현하기 위해서는 파일을 읽고 쓰는 기능이 필요합니다. 파이썬에서는 파일 입출력을 위한 내장 함수인 open()과 close()를 제공합니다. open() 함수를 사용하여 파일을 열고, close() 함수를 사용하여 파일을 닫습니다.

코드 구현

이제 설계한 기능을 바탕으로 파이썬 코드를 작성해보겠습니다. 아래 코드는 간단한 메모장을 만드는 코드입니다.

import tkinter as tk

def save():
    with open("memo.txt", "w") as f:
        memo = memo_entry.get()
        f.write(memo)

root = tk.Tk()
root.title("메모장")

memo_label = tk.Label(root, text="메모:")
memo_label.grid(row=0, column=0)

memo_entry = tk.Entry(root)
memo_entry.grid(row=0, column=1)

save_button = tk.Button(root, text="저장", command=save)
save_button.grid(row=1, column=0)

root.mainloop()

코드를 구현하는 방법은 다양합니다. 위 코드는 가장 기본적인 메모장을 구현한 예시입니다. 사용자가 원하는 기능을 추가하여 더욱 유용한 메모장을 만들어보세요. 예를 들어, 파일을 열어서 이전에 작성한 메모를 확인하거나 삭제하는 기능을 추가할 수 있습니다.

추가 기능 구현

이전에 작성한 메모를 확인하거나 삭제하는 기능을 추가해보겠습니다. 이를 위해 버튼을 두 개 추가하고, 해당 버튼을 누르면 이전에 작성한 메모를 불러오거나 삭제하도록 코드를 추가합니다.

import tkinter as tk

def save():
    with open("memo.txt", "w") as f:
        memo = memo_entry.get()
        f.write(memo)

def load():
    try:
        with open("memo.txt", "r") as f:
            memo = f.read()
            memo_entry.delete(0, tk.END)
            memo_entry.insert(0, memo)
    except FileNotFoundError:
        pass

def delete():
    try:
        with open("memo.txt", "w") as f:
            pass
        memo_entry.delete(0, tk.END)
    except FileNotFoundError:
        pass

root = tk.Tk()
root.title("메모장")

memo_label = tk.Label(root, text="메모:")
memo_label.grid(row=0, column=0)

memo_entry = tk.Entry(root)
memo_entry.grid(row=0, column=1)

save_button = tk.Button(root, text="저장", command=save)
save_button.grid(row=1, column=0)

load_button = tk.Button(root, text="불러오기", command=load)
load_button.grid(row=1, column=1)

delete_button = tk.Button(root, text="삭제", command=delete)
delete_button.grid(row=1, column=2)

root.mainloop()

위 코드는 이전에 작성한 메모를 불러오거나 삭제하는 기능을 추가한 코드입니다. 불러오기 버튼을 누르면 이전에 작성한 메모가 텍스트 상자에 불러와지며, 삭제 버튼을 누르면 메모를 삭제하고 텍스트 상자를 초기화합니다.

업그레이드 버전 구현

현재 구현된 메모장은 단순히 저장, 불러오기, 삭제의 기능만 가지고 있습니다. 더욱 유용하게 사용할 수 있는 메모장으로 업그레이드 해보겠습니다.

목록 기능 추가

먼저, 메모 리스트를 보여주는 기능을 추가해보겠습니다. 메모 리스트를 보여주기 위해서는 저장한 메모들을 파일에서 불러와야 합니다. 이를 위해 메모 리스트를 불러오는 함수를 추가하고, 해당 함수를 호출하여 저장한 메모 리스트를 보여줍니다.

def show_list():
    memo_list.delete(0, tk.END)
    try:
        with open("memo.txt", "r") as f:
            memos = f.readlines()
            for memo in memos:
                memo_list.insert(tk.END, memo)
    except FileNotFoundError:
        pass

root = tk.Tk()
root.title("메모장")

memo_label = tk.Label(root, text="메모:")
memo_label.grid(row=0, column=0)

memo_entry = tk.Entry(root)
memo_entry.grid(row=0, column=1)

save_button = tk.Button(root, text="저장", command=save)
save_button.grid(row=1, column=0)

load_button = tk.Button(root, text="불러오기", command=load)
load_button.grid(row=1, column=1)

delete_button = tk.Button(root, text="삭제", command=delete)
delete_button.grid(row=1, column=2)

memo_list = tk.Listbox(root)
memo_list.grid(row=2, column=0, columnspan=3)

show_list_button = tk.Button(root, text="목록 보기", command=show_list)
show_list_button.grid(row=3, column=0, columnspan=3)

root.mainloop()

위 코드에서는 메모 리스트를 보여주는 기능을 추가하고, 이전에 추가한 불러오기, 삭제 기능을 수정하여 메모 리스트에 있는 항목을 선택하면 해당 메모를 불러오거나 삭제할 수 있도록 코드를 수정하였습니다.

검색 기능 추가

또한, 저장된 메모 중에서 특정한 메모를 검색하는 기능을 추가해보겠습니다. 검색 기능을 추가하기 위해서는 검색어를 입력하는 텍스트 상자와 검색 버튼을 추가하고, 해당 버튼을 누르면 검색어와 일치하는 메모를 찾아서 리스트에 보여줍니다.

def search():
    memo_list.delete(0, tk.END)
    keyword = search_entry.get()
    try:
        with open("memo.txt", "r") as f:
            memos = f.readlines()
            for memo in memos:
                if keyword in memo:
                    memo_list.insert(tk.END, memo)
    except FileNotFoundError:
        pass

search_label = tk.Label(root, text="검색어:")
search_label.grid(row=4, column=0)

search_entry = tk.Entry(root)
search_entry.grid(row=4, column=1)

search_button = tk.Button(root, text="검색", command=search)
search_button.grid(row=4, column=2)

위 코드에서는 검색 기능을 추가하고, 이전에 추가한 목록 보기 기능과 검색 기능을 함께 사용할 수 있도록 코드를 수정하였습니다.

결론

위 코드를 실행하면 더욱 유용한 메모장을 사용할 수 있습니다. 이를 바탕으로 사용자 요구에 맞게 기능을 추가 및 수정하여 더욱 유용한 메모장을 만들어보세요.

Posted by goodfeel
,