下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Python中使用tkFileDialog实现文件选择、保存和路径选择目录使用tkFileDialog实现文件选择、保存和路径选择概述示例ImportError:NomodulenamedtkFileDialog问题原因验证解决方法
使用tkFileDialog实现文件选择、保存和路径选择
概述
看了下Tkinter的文档,对于Pop-updialog有三类,现在用到的是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
importTkinter,Tkconstants,tkFileDialog
classTkFileDialogExample(Tkinter.Frame):
def__init__(self,root):
Tkinter.Frame.__init__(self,root)
#optionsforbuttons
button_opt={'fill':Tkconstants.BOTH,'padx':5,'pady':5}
#definebuttons
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)
#defineoptionsforopeningorsavingafile
self.file_opt=options={}
options['defaultextension']='.txt'
options['filetypes']=[('allfiles','.*'),('textfiles','.txt')]
options['initialdir']='C:\\'
options['initialfile']='myfile.txt'
options['parent']=root
options['title']='Thisisatitle'
#ThisisonlyavailableontheMacintosh,andonlywhenNavigationServicesareinstalled.
#options['message']='message'
#ifyouusethemultiplefileversionofthemodulefunctionsthisoptionissetautomatically.
#options['multiple']=1
#definingoptionsforopeningadirectory
self.dir_opt=options={}
options['initialdir']='C:\\'
options['mustexist']=False
options['parent']=root
options['title']='Thisisatitle'
defaskopenfile(self):
"""Returnsanopenedfileinreadmode."""
returntkFileDialog.askopenfile(mode='r',**self.file_opt)
defaskopenfilename(self):
"""Returnsanopenedfileinreadmode.
Thistimethedialogjustreturnsafilenameandthefileisopenedbyyourowncode.
"""
#getfilename
filename=tkFileDialog.askopenfilename(**self.file_opt)
#openfileonyourown
iffilename:
returnopen(filename,'r')
defasksaveasfile(self):
"""Returnsanopenedfileinwritemode."""
returntkFileDialog.asksaveasfile(mode='w',**self.file_opt)
defasksaveasfilename(self):
"""Returnsanopenedfileinwritemode.
Thistimethedialogjustreturnsafilenameandthefileisopenedbyyourowncode.
"""
#getfilename
filename=tkFileDialog.asksaveasfilename(**self.file_opt)
#openfileonyourown
iffilename:
returnopen(filename,'w')
defaskdirectory(self):
"""Returnsaselecteddirectoryname."""
returntkFileDialog.askdirectory(**self.dir_opt)
if__name__=='__main__':
root=Tkinter.Tk()
TkFileDialogExample(root).pack()
root.mainloop()
ImportError:NomodulenamedtkFileDialog问题
原因
python2和pyton3的版本问题。python3之后的版本自带有tkinter.
验证
import_tkinterimporttkintertkinter._test()
在python3中输入以上命令进行验证。
解决方法
Python2中应该写成
fromtkFileDialogimportaskdirectory
python3中应该写
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 售后服务单填写模板快速响应与问题解决
- 人力资源招聘与培训计划制定工具包
- 合作项目开发进度延迟的说明函5篇范本
- 个人数据信息正确使用防护承诺书(4篇)
- 知识管理知识库构建模板
- 宁波地区宁海县2026届初三下学期期中考试英语试题(A)含解析
- 2026年江西专版市级名校初三下期第二次周考英语试题含解析
- 城市绿化养护管理规范流程指南
- 扬州市2026届初三下-期末联考英语试题试卷含解析
- 税务合规性保证承诺函(7篇)
- GB/T 191-2025包装储运图形符号标志
- GB/T 17780.5-2012纺织机械安全要求第5部分:机织和针织准备机械
- FZ/T 73001-2016袜子
- 远景变桨控制系统介绍及典型故障案例讲解
- 试验8家鸽解剖课件
- 洪湖姓氏志-黎氏的迁徙分布
- 《小小鞋店》 教学课件
- 全绝缘铜管母线安装方案
- 脓毒症并脓毒症休克指南课件
- 《定量遥感》课程教学大纲
- 公考常识40000题及答案
评论
0/150
提交评论