python 打開文件對話框相關

環境: window7python

版本: python 2.7oop

 

假設咱們有以下這樣的需求,若是所示:spa

1. 經過打開文件,咱們能夠打開指定的文件,並將其路徑顯示到輸入框中3d

2. 經過打開目錄,咱們能夠打開指定的目錄,並將其路徑顯示到輸入框中code

3. 經過另存爲,咱們能夠文件名另存爲指定的目錄或文件中。blog

咱們能夠使用python自帶的模塊:tkFileDialog其相關的方法示例,可參考:https://www.programcreek.com/python/index/671/tkfiledialogutf-8

 

爲了知足如上需求,咱們會使用到tkFileDialog的三個方法,爲了講解方便,咱們會附上代碼說明:字符串

打開文件:get

# 打開文件
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)

其效果圖以下:it

打開目錄

# 打開目錄
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()
相關文章
相關標籤/搜索