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. Slider.js轻量级图片播放控件

    Slider.js基于HTML5和CSS3实现的Slideshow 1.Slider.js 是一个图片播放Slideshow引擎,采用jQuery.CSS3和HTML5 canvas技术实现. 2.可 ...

  2. SQLServer中char、varchar、nchar、nvarchar的区别:

    (1)       定义: char:    固定长度,存储ANSI字符,不足的补英文半角空格. nchar:   固定长度,存储Unicode字符,不足的补英文半角空格 varchar:  可变长度 ...

  3. 最小生成树(HDOJ 1863)

    畅通工程 http://acm.hdu.edu.cn/showproblem.php?pid=1863 1.Prim算法: Prim算法是由一个点(最初的集合)向外延伸,找到与集合相连权值最小的边, ...

  4. SQL时间戳的使用

    SQL时间戳的使用 一直对时间戳这个概念比较模糊,相信有很多朋友也都会误认为:时间戳是一个时间字段,每次增加数据时,填入当前的时间值.其实这误导了很多朋友. 1.基本概念 时间戳:数据库中自动生成的唯 ...

  5. Android支付之支付宝封装类

    今天介绍下在android中如何集成支付宝支付到自己的APP中去.让APP能够拥有方便,快捷的支付功能. 我们在做Android支付的时候肯定会用到支付宝支付,根据官方给出的demo做起来非常费劲,所 ...

  6. sql server 2008 express 使用ip登陆 error:40 错误:2

    如图 更好的方法是将IPAll中的TCP端口设置为1433.

  7. Selenium2+python自动化5-操作浏览器基本方法

    前言 前面已经把环境搭建好了,这从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可视化工具,我们要学的是 ...

  8. Shi-Tomasi角点检测

    代码示例: #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #inc ...

  9. "2013":爱你不容易

    2013对我来说确实像年初时曾给自己定义的那样,真的是非常不平常的一年.依稀记得去年年终时,BOSS和我深聊了1多钟头,谈到职业规划.人生还有家庭的林林种种.春节在家时也仔细考虑过2013自己该如何规 ...

  10. HtDP读后感

    我小时候很喜欢电脑. 不是听音乐,也不是玩游戏.纯粹是好奇:为什么一部冷冰冰的机器插上电源之后居然能如此强大.我每个月都期盼着订阅的电脑杂志快点到,到手以后总先上手试试里面的"奇淫巧技&qu ...