tkinter 文件夹_python读取一个文件夹和其中的文件

tkinter 文件夹_python读取一个文件夹和其中的文件tkinter.filedialog.asksaveasfilename():选择以什么文件名保存,返回文件名 tkinter.filedialog.asksaveasfile():选择以什么文

tkinter

  • tkinter.filedialog.asksaveasfilename():选择以什么文件名保存,返回文件名
  • tkinter.filedialog.asksaveasfile():选择以什么文件保存,创建文件并返回文件流对象
  • tkinter.filedialog.askopenfilename():选择打开什么文件,返回文件名
  • tkinter.filedialog.askopenfile():选择打开什么文件,返回IO流对象
  • tkinter.filedialog.askdirectory():选择目录,返回目录名
  • tkinter.filedialog.askopenfilenames():选择打开多个文件,以元组形式返回多个文件名
  • tkinter.filedialog.askopenfiles():选择打开多个文件,以列表形式返回多个IO流对象

 

 

option参数如下:

defaultextension = s   默认文件的扩展名

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

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

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

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

title = T  弹出对话框的标题

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

 

#coding=UTF-8  
# __author__ = '极致'  
  
import Tkinter, Tkconstants, tkFileDialog  
  
class TkFileDialogExample(Tkinter.Frame):  
  
    def __init__(self, root):  
  
        Tkinter.Frame.__init__(self, root)  
  
        # options for buttons  
        button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}  
  
        # define buttons  
        Tkinter.Button(self, text='askopenfile', command=self.askopenfile).pack(**button_opt)  
        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 for opening or saving a file  
        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'  
  
        # This is only available on the Macintosh, and only when Navigation Services are installed.  
        #options['message'] = 'message'  
  
        # if you use the multiple file version of the module functions this option is set automatically.  
        #options['multiple'] = 1  
  
        # 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):  
  
        """Returns an opened file in read mode."""  
  
        return tkFileDialog.askopenfile(mode='r', **self.file_opt)  
  
    def askopenfilename(self):  
  
        """Returns an opened file in read mode. 
        This time the dialog just returns a filename and the file is opened by your own code. 
        """  
  
        # get filename  
        filename = tkFileDialog.askopenfilename(**self.file_opt)  
  
        # open file on your own  
        if filename:  
            return open(filename, 'r')  
  
    def asksaveasfile(self):  
  
        """Returns an opened file in write mode."""  
  
        return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)  
  
    def asksaveasfilename(self):  
  
        """Returns an opened file in write mode. 
        This time the dialog just returns a filename and the file is opened by your own code. 
        """  
  
        # get filename  
        filename = tkFileDialog.asksaveasfilename(**self.file_opt)  
  
        # open file on your own  
        if filename:  
            return open(filename, 'w')  
  
    def askdirectory(self):  
  
        """Returns a selected directoryname."""  
  
        return tkFileDialog.askdirectory(**self.dir_opt)  
  
if __name__ == '__main__':  
    root = Tkinter.Tk()  
    TkFileDialogExample(root).pack()  
    root.mainloop()  

 

今天的文章tkinter 文件夹_python读取一个文件夹和其中的文件分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/61600.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注