본문 바로가기
Python/파이썬 프로그래밍 연습

파이썬으로 간단한 메모장 만들기

by 가므자 2012. 4. 25.
[소스]
#edit.py
#encoding=utf-8

from Tkinter import *
from tkFileDialog import *
from tkMessageBox import *

class MyEdit(Frame):
def __init__(self, root=Tk()):
Frame.__init__(self, root)
self.pack(expand=YES, fill=BOTH)
self.createWidgets()
self.onNew()
#root.protocol("WM_DELETE_WINDOW", self.onExit)
self.root=root

def createWidgets(self):
self.menubar=Menu(self)
self.master.config(menu=self.menubar)

file = Menu(self.menubar)
file.add_command(label='새로 만들기', command=self.onNew)
file.add_command(label='열기', command=self.onOpen)
file.add_command(label='저장', command=self.onSave)
file.add_command(label='다른이름으로 저장...', command=self.onSaveAs)
file.add_separator()
file.add_command(label='끝내기', command=self.onExit)
self.menubar.add_cascade(label='파일', menu=file)

help=Menu(self.menubar)
help.add_command(label='낙서장 정보', command=self.onAbout)
self.menubar.add_cascade(label='도움말', menu=help)

self.text=tbox=Text(self, relief=SUNKEN)
sbar=Scrollbar(self)
sbar.config(command=tbox.yview)
tbox.config(yscrollcommand=sbar.set)
tbox.config(font=('courier', 9, 'normal'))
sbar.pack(side=RIGHT, fill=Y)
tbox.pack(side=LEFT, expand=YES, fill=BOTH)
tbox.focus()

def onNew(self):
self.file=None
self.text.delete('1.0', END)
self.master.title('낙서장')

def onOpen(self):
self.file=askopenfilename()
if self.file:
text=open(self.file, 'r').read()
text=unicode(text, 'mbcs').encode('utf-8')
self.text.delete('1.0', END)
self.text.insert('1.0', text)
self.text.focus()
self.master.title(self.file+'-낙서장')

def onSave(self):
self.onSaveAs(self.file)

def onSaveAs(self, currfile=None):
filename=currfile or asksaveasfilename()
if filename:
text=self.text.get('1.0', END+'-1c')
text=text.encode('mbcs')
open(filename, 'w').write(text)
self.file=filename
self.master.title(self.file+'-낙서장')

def onExit(self):
self.root.destroy()
self.root.quit()

def onAbout(self):
showinfo('낙서장', '낙서장\n', '만든 날 : 2011년 8월 31일 \n' '만든 이 : lexlove')



if __name__ == '__main__' : MyEdit().mainloop()

 

댓글