[Python 2.7] 파일 다이얼로그
다음 링크는 unPythonic.net의 문서입니다.
Tkinter의 tkFileDialog 클래스에 대해 비교적 잘 설명한 좋은 문서입니다.
또한 Tkinter를 클래스로 처리하는 좋은 방법을 소개하고 있습니다.
모범적인 코딩이라 볼 수 있습니다.
http://tkinter.unpythonic.net/wiki/tkFileDialog
참고로 코드를 아래에 옮겨놓았습니다.
import Tkinter,Tkconstants,tkFileDialog
class TkFileDialogExample(Tkinter.Frame):
def __init__(self,root):
Tkinter.Frame.__init__(self,root)
button_opt={'fill':Tkconstants.BOTH,'padx':5,'pady':5}
Tkinter.Button(self,text="askopenfile",command=self.askopenfile).pack(**button_opt)
#**: callback of dictionary data
Tkinter.Button(self,text="askopenfilename",command=self.askopenfilename).pack(**button_opt)
Tkinter.Button(self,text="asksaveasfile",command=self.asksaveasfile).pack(**button_opt)
Tkinter.Button(self,text="asksaveasfilename",command=self.asksaveasfilename).pack(**button_opt)
Tkinter.Button(self,text="askdirectory",command=self.askdirectory).pack(**button_opt)
# define options
self.file_opt=options={}
options['defaultextension']='.txt'
options['filetypes']=[('all files',".*"),('text files','.txt')]
options['initialdir']='c:\\'
options['initialfile']='myfile.txt'
options['parent']=root
options['title']='This is a title'
#defining options for opening a directory
self.dir_opt = options={}
options['initialdir']='c:\\'
options['mustexist']=False
options['parent']=root
options['title']='This is a title'
def askopenfile(self):
return tkFileDialog.askopenfile(mode='r',**self.file_opt)
def askopenfilename(self):
filename=tkFileDialog.askopenfilename(**self.file_opt)
if filename:
return open(filename,'r')
def asksaveasfile(self):
return tkFileDialog.asksaveasfile(mode='w',**self.file_opt)
def asksaveasfilename(self):
filename=tkFileDialog.asksaveasfilename(**self.file_opt)
if filename:
return open(filename,'w')
def askdirectory(self):
return tkFileDialog.askdirectory(**self.dir_opt)
if __name__=="__main__":
root=Tkinter.Tk()
TkFileDialogExample(root).pack()
root.mainloop()
Tkinter의 tkFileDialog 클래스에 대해 비교적 잘 설명한 좋은 문서입니다.
또한 Tkinter를 클래스로 처리하는 좋은 방법을 소개하고 있습니다.
모범적인 코딩이라 볼 수 있습니다.
http://tkinter.unpythonic.net/wiki/tkFileDialog
참고로 코드를 아래에 옮겨놓았습니다.
import Tkinter,Tkconstants,tkFileDialog
class TkFileDialogExample(Tkinter.Frame):
def __init__(self,root):
Tkinter.Frame.__init__(self,root)
button_opt={'fill':Tkconstants.BOTH,'padx':5,'pady':5}
Tkinter.Button(self,text="askopenfile",command=self.askopenfile).pack(**button_opt)
#**: callback of dictionary data
Tkinter.Button(self,text="askopenfilename",command=self.askopenfilename).pack(**button_opt)
Tkinter.Button(self,text="asksaveasfile",command=self.asksaveasfile).pack(**button_opt)
Tkinter.Button(self,text="asksaveasfilename",command=self.asksaveasfilename).pack(**button_opt)
Tkinter.Button(self,text="askdirectory",command=self.askdirectory).pack(**button_opt)
# define options
self.file_opt=options={}
options['defaultextension']='.txt'
options['filetypes']=[('all files',".*"),('text files','.txt')]
options['initialdir']='c:\\'
options['initialfile']='myfile.txt'
options['parent']=root
options['title']='This is a title'
#defining options for opening a directory
self.dir_opt = options={}
options['initialdir']='c:\\'
options['mustexist']=False
options['parent']=root
options['title']='This is a title'
def askopenfile(self):
return tkFileDialog.askopenfile(mode='r',**self.file_opt)
def askopenfilename(self):
filename=tkFileDialog.askopenfilename(**self.file_opt)
if filename:
return open(filename,'r')
def asksaveasfile(self):
return tkFileDialog.asksaveasfile(mode='w',**self.file_opt)
def asksaveasfilename(self):
filename=tkFileDialog.asksaveasfilename(**self.file_opt)
if filename:
return open(filename,'w')
def askdirectory(self):
return tkFileDialog.askdirectory(**self.dir_opt)
if __name__=="__main__":
root=Tkinter.Tk()
TkFileDialogExample(root).pack()
root.mainloop()
댓글
댓글 쓰기