Python中使用tkFileDialog实现文件选择、保存和路径选择

作者:blingbling_110 时间:2022-02-17 09:35:32 

使用tkFileDialog实现文件选择、保存和路径选择

概述

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

tkFileDialog有三种形式:

  • 一个是:askopenfilename(option=value, …) 这个是”打开”对话框

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

  • 另一个是:askdirectory()这个是路径选择对话框

option参数如下:

  • defaultextension = s 默认文件的扩展名

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

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

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

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

  • title = T 弹出对话框的标题

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

示例

#coding=UTF-8    
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()  

ImportError: No module named 'tkFileDialog'问题

原因

python2和pyton3的版本问题。python3之后的版本自带有tkinter.

验证

  • import _tkinter

  • import tkinter

  • tkinter._test()

在python3中输入以上命令进行验证。

解决方法

Python2中应该写成  

from tkFileDialog import askdirectory

python3中应该写成  

from tkinter.filedialog import askdirectory

tkColorChooser ------------>tkinter.colorchooser
tkCommonDialog --------------->tkinter.commondialog   

其他的可以类推。

来源:https://blog.csdn.net/weixin_44256803/article/details/86571383

标签:tkFileDialog,文件选择,保存,路径选择
0
投稿

猜你喜欢

  • 网页新窗口打开好不好,数据说话

    2008-05-31 17:01:00
  • JavaScript中的函数声明和函数表达式区别浅析

    2023-08-05 23:22:36
  • 浅谈Python的list中的选取范围

    2023-06-23 01:58:01
  • 让复杂导航设计变得简单

    2008-01-07 11:50:00
  • python中的随机数种子seed()用法说明

    2021-11-15 17:16:34
  • SQL Server数据库简体繁体数据混用的问题

    2008-12-05 16:07:00
  • 有关于IE8 Beta 1两个提醒

    2008-05-15 12:32:00
  • 利用Python查看目录中的文件示例详解

    2023-02-06 14:13:28
  • python脚本第一行如何写

    2021-03-24 22:56:32
  • PHP header()函数常用方法总结

    2023-09-06 16:51:50
  • opencv python Canny边缘提取实现过程解析

    2023-08-20 19:43:18
  • 3分钟看懂Python后端必须知道的Django的信号机制

    2022-08-17 18:17:55
  • ASP开发中可能遇到的错误信息中文说明大全(整理收集)第1/2页

    2010-07-02 09:50:31
  • 基于FlashPaper实现JSP在线阅读代码示例

    2023-06-14 21:02:31
  • 用js实现放大镜效果

    2023-09-19 18:29:29
  • Python类中的魔法方法之 __slots__原理解析

    2023-10-01 18:49:51
  • 在opera里css出现渲染问题

    2009-01-15 12:19:00
  • python得到电脑的开机时间方法

    2021-01-05 08:00:05
  • Python实现列表转换成字典数据结构的方法

    2023-03-17 22:59:09
  • python 提取key 为中文的json 串方法

    2022-02-08 04:55:06
  • asp之家 网络编程 m.aspxhome.com