python 打开文件对话框相关

环境: window7

版本: python 2.7

假设我们有如下这样的需求,如果所示:

1. 通过打开文件,我们可以打开指定的文件,并将其路径显示到输入框中

2. 通过打开目录,我们可以打开指定的目录,并将其路径显示到输入框中

3. 通过另存为,我们可以文件名另存为指定的目录或文件中。

我们可以使用python自带的模块:tkFileDialog其相关的方法示例,可参考:https://www.programcreek.com/python/index/671/tkfiledialog

为了满足如上需求,我们会使用到tkFileDialog的三个方法,为了讲解方便,我们会附上代码说明:

打开文件:

# 打开文件
def openFileEvent(self):
    # 参数相关:
    # defaultextension: 默认文件的扩展名
    # filetypes: 设置文件类型下拉菜单里的的选项
    # initialdir: 对话框中默认的路径
    # initialfile: 对话框中初始化显示的文件名
    # parent: 父对话框(由哪个窗口弹出就在哪个上端)
    # title: 弹出对话框的标题

    # 选择文件后,会返回显示文件的完整路径,取消的话,会返回空字符串
    newDir = tkFileDialog.askopenfilename(initialdir='C:\Python27',
        initialfile='README',title='打开新文件')
    if len(newDir) == 0:
        return
    # 显示新路径内容
    self.csdVar.set(newDir)

其效果图如下:

打开目录

# 打开目录
def openDirEvent(self):
    # 参数相关
    #title: 弹出对话框的标题
    #initialdir:  对话框中默认的路径
    newDir = tkFileDialog.askdirectory(initialdir='C:\Python27',title='打开目录')
    # 判定是否选择了文件夹,如果已选择则返回其路径,否则返回空字符
    if len(newDir) == 0:
        return
    # 显示内容
    self.csdVar.set(newDir

效果图如下:

另存为

# 另存为
def selectOutPathEvent(self):
    # 其参数与askopenfilename一样,不再赘述
    newDir = tkFileDialog.asksaveasfilename(initialdir='C:\Python27')
    if len(newDir) == 0:
        return
    self.outVar.set(newDir)

效果图如下:

 

其完整的示例Demo:

# -*- coding:utf-8 -*-
# __author__ = 'Code~'

import traceback
import Tkinter
from Tkinter import *
import tkFileDialog

DEF_PATH = 'C:\Python27'
# 消息框模块
class MsgBox:
    def __init__(self):
        # 目录相关
        self.root = Tkinter.Tk()
        self.showMsgBox()
        self.root.mainloop()
    
    # 显示消息框
    def showMsgBox(self):
        root = self.root           
 
        #
        Label(root, text='源文件:').grid(row=1, column=0, sticky='E')
        self.csdVar = StringVar()
        self.csdVar.set(DEF_PATH)
        Entry(root, textvariable=self.csdVar, width=50).grid(row=1, column=1)
        Button(root, text='打开文件', command=self.openFileEvent).grid(row=1, column=2)
        Button(root, text='打开目录', command=self.openDirEvent).grid(row=1, column=3)
        
        #
        Label(root, text='输出:').grid(row=2, column=0, sticky='E')
        self.outVar = StringVar()
        self.outVar.set(DEF_PATH)
        Entry(root,textvariable=self.outVar, width=50).grid(row=2, column=1)
        Button(root,text='另存为',command=self.selectOutPathEvent).grid(row=2, column=2)

    
    # 打开文件
    def openFileEvent(self):
        newDir = tkFileDialog.askopenfilename(initialdir=DEF_PATH,title='打开新文件')
        if len(newDir) == 0:
            return
        self.csdVar.set(newDir)
    # 打开目录
    def openDirEvent(self):
        newDir = tkFileDialog.askdirectory(initialdir=DEF_PATH,title='打开目录')
        if len(newDir) == 0:
            return
        self.csdVar.set(newDir)

    # 另存为
    def selectOutPathEvent(self):
        newDir = tkFileDialog.asksaveasfilename(initialdir=DEF_PATH)
        if len(newDir) == 0:
            return
        self.outVar.set(newDir)


if __name__  == "__main__":
    try: 
        messageBox = MsgBox()
    except Exception:
        traceback.print_exc()

猜你喜欢

转载自www.cnblogs.com/SkyflyBird/p/10344019.html