Python中使用tkFileDialog实现文件选择、保存和路径选择_第1页
Python中使用tkFileDialog实现文件选择、保存和路径选择_第2页
Python中使用tkFileDialog实现文件选择、保存和路径选择_第3页
Python中使用tkFileDialog实现文件选择、保存和路径选择_第4页
Python中使用tkFileDialog实现文件选择、保存和路径选择_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论