python文件选择:tkFileDialog 基础

看了下Tkinter的文档,对于Pop-up dialog有三类,现在用到的是tkFileDialog

tkFileDialog有两种形式: 

一个是.askopenfilename(option=value, ...) 这个是"打开"对话框

另一个是:asksaveasfilename(option=value, ...) 这个是另存为对话框

option参数如下:

defaultextension = s   默认文件的扩展名

filetypes = [(label1, pattern1), (label2, pattern2), ...]   设置文件类型下拉菜单里的的选项

initialdir = D  对话框中默认的路径

initialfile = F  对话框中初始化显示的文件名

parent = W  父对话框(由哪个窗口弹出就在哪个上端)

title = T  弹出对话框的标题

如果选中文件的话,确认后会显示文件的完整路径,否则单击取消的话会返回空字符串

[python] view plain copy
  1. #coding=UTF-8  
  2. # __author__ = '极致'  
  3.   
  4. import Tkinter, Tkconstants, tkFileDialog  
  5.   
  6. class TkFileDialogExample(Tkinter.Frame):  
  7.   
  8.     def __init__(self, root):  
  9.   
  10.         Tkinter.Frame.__init__(self, root)  
  11.   
  12.         # options for buttons  
  13.         button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}  
  14.   
  15.         # define buttons  
  16.         Tkinter.Button(self, text='askopenfile', command=self.askopenfile).pack(**button_opt)  
  17.         Tkinter.Button(self, text='askopenfilename', command=self.askopenfilename).pack(**button_opt)  
  18.         Tkinter.Button(self, text='asksaveasfile', command=self.asksaveasfile).pack(**button_opt)  
  19.         Tkinter.Button(self, text='asksaveasfilename', command=self.asksaveasfilename).pack(**button_opt)  
  20.         Tkinter.Button(self, text='askdirectory', command=self.askdirectory).pack(**button_opt)  
  21.   
  22.         # define options for opening or saving a file  
  23.         self.file_opt = options = {}  
  24.         options['defaultextension'] = '.txt'  
  25.         options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]  
  26.         options['initialdir'] = 'C:\'  
  27.         options['initialfile'] = 'myfile.txt'  
  28.         options['parent'] = root  
  29.         options['title'] = 'This is a title'  
  30.   
  31.         # This is only available on the Macintosh, and only when Navigation Services are installed.  
  32.         #options['message'] = 'message'  
  33.   
  34.         # if you use the multiple file version of the module functions this option is set automatically.  
  35.         #options['multiple'] = 1  
  36.   
  37.         # defining options for opening a directory  
  38.         self.dir_opt = options = {}  
  39.         options['initialdir'] = 'C:\'  
  40.         options['mustexist'] = False  
  41.         options['parent'] = root  
  42.         options['title'] = 'This is a title'  
  43.   
  44.     def askopenfile(self):  
  45.   
  46.         """Returns an opened file in read mode."""  
  47.   
  48.         return tkFileDialog.askopenfile(mode='r', **self.file_opt)  
  49.   
  50.     def askopenfilename(self):  
  51.   
  52.         """Returns an opened file in read mode. 
  53.         This time the dialog just returns a filename and the file is opened by your own code. 
  54.         """  
  55.   
  56.         # get filename  
  57.         filename = tkFileDialog.askopenfilename(**self.file_opt)  
  58.   
  59.         # open file on your own  
  60.         if filename:  
  61.             return open(filename, 'r')  
  62.   
  63.     def asksaveasfile(self):  
  64.   
  65.         """Returns an opened file in write mode."""  
  66.   
  67.         return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)  
  68.   
  69.     def asksaveasfilename(self):  
  70.   
  71.         """Returns an opened file in write mode. 
  72.         This time the dialog just returns a filename and the file is opened by your own code. 
  73.         """  
  74.   
  75.         # get filename  
  76.         filename = tkFileDialog.asksaveasfilename(**self.file_opt)  
  77.   
  78.         # open file on your own  
  79.         if filename:  
  80.             return open(filename, 'w')  
  81.   
  82.     def askdirectory(self):  
  83.   
  84.         """Returns a selected directoryname."""  
  85.   
  86.         return tkFileDialog.askdirectory(**self.dir_opt)  
  87.   
  88. if __name__ == '__main__':  
  89.     root = Tkinter.Tk()  
  90.     TkFileDialogExample(root).pack()  
  91.     root.mainloop()  
原文地址:https://www.cnblogs.com/monsteryang/p/6558899.html