Quiz

jurin·2020년 9월 26일
0

##################################################################
나도 코딩 강의를 듣고 정리한 내용입니다.
출처 : 나도코딩 https://www.youtube.com/channel/UC7iAOLiALt2rtMVAWWl4pnw
##################################################################

Quiz) tkinter 를 이용한 메모장 프로그램을 만드시오.

[GUI 조건]
1. title : 제목 없음 - windows 메모장
2. 메뉴 : 파일, 편집, 서식, 보기, 도움말
3. 실제 메뉴 구현 : 파일 메뉴 내에서 열기, 저장, 끝내기 3개만 처리
3-1. 열기 : mynote.txt 파일 내용 열어서 보여주기
3-2. 저장 : mynote.txt 파일에 현재 내용 저장하기
3-3. 끝내기 : 프로그램 종료
4. 프로그램 시작 시 본문은 비어 있는 상태
5. 하단 status 바는 필요 없음
6. 프로그램 크기, 위치는 자유롭게 하되 크기 조정 가능해야 함
7. 본문 우측에 상하 스크롤 바 넣기

import os
import tkinter.ttk as ttk
from tkinter import *

root = Tk()
root.title("제목 없음 - windows 메모장")
root.geometry("640x480")

import tkinter.ttk as ttk


# 열기, 저장 파일 이름
filename = "mynote.txt"


def file_open():
    if os.path.isfile(filename): # 파일 있으면 True, 없으면 False
        with open(filename, "r", encoding="utf8") as file:
            txt.delete("1.0", END) # 텍스트 위젯 본문 삭제
            txt.insert(END, file.read()) # 파일 내용을 본문에 입력

def file_quit():
    quit()

def file_save():
    with open(filename, "w", encoding="utf8") as file:
        file.write(txt.get("1.0", END)) # 모든 내용을 가져와서 저장

menu = Menu(root)

# 파일
menu_file = Menu(menu, tearoff=0)
menu_file.add_command(label="열기", command=file_open)
menu_file.add_command(label="저장", command=file_save)
menu_file.add_separator()
menu_file.add_command(label="끝내기", command=file_quit)


# 편집
menu_edit = Menu(menu, tearoff=0)

# 서식
menu_format = Menu(menu, tearoff=0)

# 보기
menu_view = Menu(menu, tearoff=0)

# 도움말
menu_help = Menu(menu, tearoff=0)



menu.add_cascade(label="파일", menu=menu_file)
menu.add_cascade(label="편집", menu=menu_edit)
menu.add_cascade(label="서식", menu=menu_format)
menu.add_cascade(label="보기", menu=menu_view)
menu.add_cascade(label="도움말", menu=menu_help)


# 스크롤바
scrollbar = Scrollbar(root)
scrollbar.pack(side="right", fill="y")

# 본문 영역
txt = Text(root, yscrollcommand=scrollbar.set, width=200, height=100)
txt.pack()

scrollbar.config(command=txt.yview)

root.config(menu=menu)
root.mainloop()
profile
anaooauc1236@naver.com

0개의 댓글