wxPython控件学习之wx.FileDialog

1. wx.FileDialog 允许用户从系统的文件中选择一个或者多个文件。支持通配符,可以让用户选择关心的文件。例如:"BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"只会显示和选择图片后缀类型是bmp 和gif。

2. Window Styles

wx.FD_OPEN 单个文件选择对话框
wx.FD_SAVE 文件保存对话框
wx.FD_OVERWRITE_PROMPT 只对于保存对话框有效,当覆盖文件的时候,会弹出提醒对话框
wx.FD_MULTIPLE 只对于打开对话框有效,支持多选
wx.FD_CHANGE_DIR 改变当前工作目录为用户选择的文件夹

3. 方法集:

 

Method Summary
FileDialog

__init__(self, parent, message, defaultDir, defaultFile, wildcard, style, pos) 
parent:父窗口。messale:显示在文件对话框的标题,defaultDir默认文件夹,defaultFile默认文件,wildcard匹配通配符,style = window style

String GetDirectory(self)  返回默认的文件夹
String GetFilename(self)   获得默认的文件名字
PyObject GetFilenames(self)  返回用户选择的文件列表
int GetFilterIndex(self) 
Returns the index into the list of filters supplied, optionally, in the wildcard parameter.
String GetMessage(self) 返回文件对话框标题
String GetPath(self) 返回选择的文件的全路径(包括文件路径和文件名)
PyObject GetPaths(self) 多选情况下,返回用户选择的文件全路径列表
String GetWildcard(self) 返回通配符
  SetDirectory(self, dir) 设置默认的文件目录
  SetFilename(self, name) 设置默认的文件名
  SetFilterIndex(self, filterIndex) 
Sets the default filter index, starting from zero.
  SetMessage(self, message) 设置对话框的标题
  SetPath(self, path) 设置默认选择的文件全路径名
  SetWildcard(self, wildCard) 设计通配符

4.例子:

a.创建app主入口程序

复制代码

#-*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:        模块shell
# Purpose:     应用程序的main入口函数
#
# Author:      ankier
#
# Created:     28-10-2012
# Copyright:   (c) ankier 2012
# Licence:     
#-------------------------------------------------------------------------------

import wx
from mainFrame import MainFrame
import sys

## @detail ShellApp主应用程序的核
class ShellApp(wx.App):
    
    def OnInit(self):
        mainFrame = MainFrame()
        mainFrame.Show(True)          
        return True
    
# @detail main程序的主入口程序   
if __name__ == '__main__':      
    app = ShellApp()
    #重新定向wxpython的输出输入和错误输出到系统标准输入输出
    sys.stdin = sys.__stdin__
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__
    app.MainLoop()

复制代码

b.MainFrame 文件选择对话框的使用

复制代码

#-*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name:        模块mainFrame
# Purpose:     应用程序的主界面
#
# Author:      ankier
#
# Created:     28-10-2012
# Copyright:   (c) ankier 2012
# Licence:     
#-------------------------------------------------------------------------------

import wx
import os

## @detail MainFrame主界面窗口类
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,  'FileDialog study', pos=wx.DefaultPosition,
            size=(800, 600), style=wx.DEFAULT_FRAME_STYLE)
        self.CreateStatusBar()
        self.__BuildMenus() 
        
        label = wx.StaticText(self, -1, u"当前选择的文件")
        textBox = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE, size =(-1, 300))    
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(label, 0, wx.ALL|wx.ALIGN_CENTRE)
        sizer.Add(textBox, 1, wx.ALL|wx.ALIGN_CENTRE)
        self.__TextBox = textBox
        self.SetSizerAndFit(sizer)
        
    def __BuildMenus(self):
        mainMenuBar = wx.MenuBar()
        
        fileMenu = wx.Menu()
        
        fileMenuItem = fileMenu.Append(-1, "打开单个文件")
        self.Bind(wx.EVT_MENU, self.__OpenSingleFile, fileMenuItem)
        
        saveMenuItem = fileMenu.Append(-1, "保存文件")
        self.Bind(wx.EVT_MENU, self.__SaveFile, saveMenuItem)
        
        savePromptMenuItem = fileMenu.Append(-1, "保存文件及提示覆盖")
        self.Bind(wx.EVT_MENU, self.__SavePromptFile, savePromptMenuItem)
        
        multipleOpenMenuItem = fileMenu.Append(-1, "多文件选择")
        self.Bind(wx.EVT_MENU, self.__MultipleSelectFiles, multipleOpenMenuItem)
        
        mainMenuBar.Append(fileMenu, title =u'&文件')
        
        self.SetMenuBar(mainMenuBar)
   
    ## @detail wx.FileDialog style:wx.FD_OPEN
    def __OpenSingleFile(self, event):
        filesFilter = "Dicom (*.dcm)|*.dcm|" "All files (*.*)|*.*"
        fileDialog = wx.FileDialog(self, message ="选择单个文件", wildcard = filesFilter, style = wx.FD_OPEN)
        dialogResult = fileDialog.ShowModal()
        if dialogResult !=  wx.ID_OK:
            return
        path = fileDialog.GetPath()
        self.__TextBox.SetLabel(path)
        
    ## @detail wx.FileDialog style:wx.FD_SAVE
    def __SaveFile(self, event):
        filesFilter = "Dicom (*.dcm)|*.dcm|" "All files (*.*)|*.*"
        fileDialog = wx.FileDialog(self, message ="保存文件", wildcard = filesFilter, style = wx.FD_SAVE)
        dialogResult = fileDialog.ShowModal()
        if dialogResult !=  wx.ID_OK:
            return
        path = fileDialog.GetPath()
        self.__TextBox.SetLabel(path)
    
    ## @detail wx.FileDialog style:wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
    def __SavePromptFile(self, event):
        filesFilter = "Dicom (*.dcm)|*.dcm|" "All files (*.*)|*.*"
        fileDialog = wx.FileDialog(self, message ="保存&prompt文件", wildcard = filesFilter, style = wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
        dialogResult = fileDialog.ShowModal()
        if dialogResult !=  wx.ID_OK:
            return
        path = fileDialog.GetPath()
        self.__TextBox.SetLabel(path)        
    
    ## @detail wx.FileDialog style:wx.FD_OPEN | wx.FD_MULTIPLE
    def __MultipleSelectFiles(self, event):
        filesFilter = "Dicom (*.dcm)|*.dcm|" "All files (*.*)|*.*"
        fileDialog = wx.FileDialog(self, message ="多文件选择", wildcard = filesFilter, style = wx.FD_OPEN|wx.FD_MULTIPLE)
        dialogResult = fileDialog.ShowModal()
        if dialogResult !=  wx.ID_OK:
            return
        paths = fileDialog.GetPaths()
        self.__TextBox.SetLabel('')
        for path in paths:
            self.__TextBox.AppendText(path+'\n')
        
        
        

复制代码

 

 

你可能感兴趣的:(个人日记,wxpython,python)