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()  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM