tkFileDialog有两种形式:

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

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

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

python文件选择:tkFileDialog 基础的更多相关文章

  1. python 文件处理(基础字符)

    基于字符read & write 最基本的文件操作当然就是在文件中读写数据.这也是很容易掌握的.现在打开一个文件以进行写操作: 1. fileHandle = open ( 'test.txt ...

  2. Python文件基础

    ===========Python文件基础========= 写,先写在了IO buffer了,所以要及时保存 关闭.关闭会自动保存. file.close() 读取全部文件内容用read,读取一行用 ...

  3. Python学习之路基础篇--08Python基础+ 文件的基本操作和 注册小作业

    1 文件的基本操作 #1. 打开文件的模式有(默认为文本模式): r ,只读模式[默认模式,文件必须存在,不存在则抛出异常] w,只写模式[不可读:不存在则创建:存在则清空内容] a, 只追加写模式[ ...

  4. 从0开始做一个的Vue图片/ 文件选择(上传)组件[基础向]

    原文:http://blog.csdn.net/sinat_17775997/article/details/58585142 之前用Vue做了一个基础的组件 vue-img-inputer ,下面就 ...

  5. Python文件基础操作(IO入门1)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6422270.html 本文出自:[Edwin博客园] Python文件基础操作(IO入门1) 1. pyth ...

  6. Python(四)基础篇之「文件对象&错误处理」

    [笔记]Python(四)基础篇之「文件对象&错误处理」 2016-12-08 ZOE    编程之魅  Python Notes: ★ 如果你是第一次阅读,推荐先浏览:[重要公告]文章更新. ...

  7. Python基础篇【第2篇】: Python文件操作

    Python文件操作 在Python中一个文件,就是一个操作对象,通过不同属性即可对文件进行各种操作.Python中提供了许多的内置函数和方法能够对文件进行基本操作. Python对文件的操作概括来说 ...

  8. Linux基础学习(一)__后台运行Python文件

    Linux 后台运行Python脚本 1.安装Python:(python 3.5.4) 2.安装Python依赖包: 2.1 处理Python更新后yum无法正常使用的问题 (错误信息: -bash ...

  9. python基础5 ---python文件处理

    python文件处理 一.文件处理的流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件的操作方法 1.文件打开模式格式: 文件句柄 = open('文件路径', ...

随机推荐

  1. 计算hashCode的常见方法

    把某个非零常数值,比如说17,保存在一个叫result的int类型的变量中. 2.对于对象中每一个关键域f(值equals方法中考虑的每一个域),完成以下步骤: a.为该域计算int类型的散列吗c: ...

  2. SQLSERVER | 查询数据库中所有的表的名字 | 查询数据库中的所有数据库名

    SQLSERVER 1.查询某个数据库中所有的表名:  SELECT Name FROM SysObjects Where XType='U' ORDER BY Name 2.查询数据库中的所有数据库 ...

  3. 配置zabbix当内存剩余不足10%的时候触发报警

    配置zabbix当内存剩余不足10%的时候触发报警 zabbix默认的剩余内存报警: Average Lack of available memory on server {HOST.NAME}{Te ...

  4. 滚动div的动画

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. css3颜色

    镂空字的2种做法

  6. 消息框用法MessageBox

    关键字:C# MessageBox 消息对话框 在程序中,我们经常使用消息对话框给用户一定的信息提示,如在操作过程中遇到错误或程序异常,经常会使用这种方式给用于以提示.在C#中,MessageBox消 ...

  7. keyup keydown keypress 区别

    测试的浏览器环境: chrome 版本 43.0.2357.134 mfirefox 版本 24.0IE6(绿色版 IE.exe)IE7IE8IE9 搜狗拼音输入法3.5(3.5.0.1089)网吧专 ...

  8. 红星美凯龙CEO车建新的圆融和霸气

    待人接物中车建新有许多习惯,与别人一起行走时,走在靠马路的一边:吃饭时最好的菜留给客人.他说,做人往往就在细节中,别小看一个举动,无意中就会感染别人.和别人在一起,你要时时刻刻先考虑对方. 细节上体察 ...

  9. Android菜鸟成长记4-button点击事件

    Button 1.button按钮的创建 一般来说,在我们新建一个Android项目的时候,会有会默认有一个activity_main.xml的文件 如果你在新建项目的时候,把Create Activ ...

  10. java基础九[网络与线程](阅读Head First Java记录)

    网络socket连接 Java API的网络功能包(java.net)已经将底层的TCP连接等都封装好了,我们只需要通过Socket对象来建立客户端和服务器的连接,然后客户端能向服务器发送请求,并接收 ...