python 选择文件_python文件选择:tkFileDialog 基础

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 文件处理(基础字符)

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

Python文件基础

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

配置SharePoint使用ADFS

1. 如果网站应用程序没有使用声明式验证 $wpp = Get-SPWebApplication $wpp.UseClaimsAuthentication = 1 $wpp.U ...

JSON:org.json的基本用法

java中用于解释json的主流工具有org.json.json-lib与gson,本文介绍org.json的应用. 官方文档: http://www.json.org/java/ http://de ...

Android开源库集锦

一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...

Udacity-Artificial Intelligence for Robotics 课程笔记

Lesson 1 Localization 蒙特卡洛机器人定位模型 sense 贝叶斯模型 move 全概率公式 localization练习 # The function localize take ...

geoserver图层属性查询及查询结果转换为arcgis js api能使用的格式

一个项目使用了ArcGIS JS API开发GIS展示层,但GIS服务使用了Geoserver,这时加载Geoserver数据和查询数据就和之前完全不一样了,以下介绍下我使用ArcGIS JS API ...

CrawlScript脚本语言实现网络爬虫

前段时间我们学习了几种爬虫技术,我们来回顾一下,webCollector,htmlParser,Jsoup,各有优劣,但是如果能灵活运用,其实都是很不错的.那么,今天呢,我们来学习一种脚本语言,这是一 ...

从蓝光到4K,腾讯视频高码率下载背后的技术

欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 由 腾讯技术工程官方号 发布在云+社区 蓝光和4k视频正逐渐普及,4K视频峰值码率超10Mbit/s.架构平台部TVideo平台从资源,链路.缓 ...

clumsy模拟客户端网络差的场景的使用

1.为什么需要模拟客户端网络差的情况? a. 研发环境的网络配置一般较高,网络响应快,不容易出现网络故障,但是客户使用的网络环境千差万别,不排除使用过程中遇到网络故障的情况. b.程序有些时候是多线程 ...

scrapy中间件

一.下载中间件 from scrapy import signals from scrapy.http import Response from scrapy.exceptions import Ig ...

vue修饰符学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值