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. C++ 约瑟夫环问题

    约瑟夫环比较经典了 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直 ...

  2. Python自动化 【第九篇】:Python基础-线程、进程及python GIL全局解释器锁

    本节内容: 进程与线程区别 线程 a)  语法 b)  join c)  线程锁之Lock\Rlock\信号量 d)  将线程变为守护进程 e)  Event事件 f)   queue队列 g)  生 ...

  3. centos7.2上实践cgoup

    基本介绍 CGroups 是一种对进程资源管理和控制的统一框架,它提供的是一种机制,而具体的策略(Policy)是通过子系统(subsystem)来完成的.子系统是CGroups对进程组进行资源控制的 ...

  4. ubuntu mysql emma中文乱码问题解决

    ubuntu mysql emma中文乱码问题解决 emma默认用apt-get 安装的话,emma是不支持中文的,配置文件或直接修改emma程序源文件(python). apt-get安装emma ...

  5. ubuntu14.04下配置使用openCV3.0

    [操  作  系  统] Ubuntu 14.04 LTS [OpenCV版本]  3.0.0-beta [Eclipse 版 本] 3.8.1 需要知识: Linux系统shell命令基础 编译原理 ...

  6. java 堆栈

    堆栈(stack).位于通用RAM中,但通过它的“堆栈指针”可以从处理器哪里获得支持.堆栈指针若向下移动,则分配新的内存:若向上移动,则释放那些 内存.这是一种快速有效的分配存储方法,仅次于寄存器. ...

  7. python 获取文件夹大小

    __author__ = 'bruce' import os from os.path import join,getsize def getdirsize(dir): size=0l for (ro ...

  8. VC++ 控制外部程序,向外部程序发送一个消息的方法

    这里需要考虑两部分的内容: 发送端: 查找对应的窗体,找到CWnd的值 向窗体发送消息 举例: CWnd* wnd = FindWindow(NULL, _T("选择题做题过程中" ...

  9. mysql事务处理用法与实例详解

    来源:转载  MySQL的事务支持不是绑定在MySQL服务器本身,而是与存储引擎相关1.MyISAM:不支持事务,用于只读程序提高性能 2.InnoDB:支持ACID事务.行级锁.并发 3.Berke ...

  10. Java集合类之ArrayList

    学习Java的集合类 (1)成员变量以及初始化 private static final int DEFAULT_CAPACITY = 10; private static final Object[ ...