wxPython控件学习之wx.FileDialog
2012-10-28 16:45
459 查看
1. wx.FileDialog 允许用户从系统的文件中选择一个或者多个文件。支持通配符,可以让用户选择关心的文件。例如:"BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"只会显示和选择图片后缀类型是bmp 和gif。
2. Window Styles
3. 方法集:
4.例子:
a.创建app主入口程序
b.MainFrame 文件选择对话框的使用
2. Window Styles
wx.FD_OPEN | 单个文件选择对话框 |
wx.FD_SAVE | 文件保存对话框 |
wx.FD_OVERWRITE_PROMPT | 只对于保存对话框有效,当覆盖文件的时候,会弹出提醒对话框 |
wx.FD_MULTIPLE | 只对于打开对话框有效,支持多选 |
wx.FD_CHANGE_DIR | 改变当前工作目录为用户选择的文件夹 |
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) |
a.创建app主入口程序
#-*- coding: UTF-8 -*- #------------------------------------------------------------------------------- # Name: 模块shell # Purpose: 应用程序的main入口函数 # # Author: ankier # # Created: 28-10-2012 # Copyright: (c) ankier 2012 # Licence: <your 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: <your 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控件学习之wx.ComboBox
- wxPython:文件对话框wx.FileDialog
- wxPython常用控件--wx.RadioButton,wx.CheckBox,wx.Choice,wx.Slider,wx.SpinCtrl,wx.Timer,wx.Dialog
- wxPython控件学习之wx.StaticText
- wxPython控件学习之wx.FlexGridSizer
- wxPython控件学习之wx.FlexGridSizer
- wxPython控件学习之wx.lib.scrolledpanel.ScrolledPanel
- wxPython控件学习之wx.grid.Grid 表格控件
- wxPython控件学习之wx.TextCtrl
- wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEditor为例)
- OpenFileDialog控件的小小学习
- wxPython:文件对话框wx.FileDialog
- wxPython控件学习之wx.BoxSizer
- wxpython之dialog学习
- C# Winform OpenFileDialog 控件
- Android基础控件之-----------对话框Dialog(学习笔记)
- OpenFileDialog控件用法
- C# OpenFileDialog 控件
- Android学习专题-控件组件篇:Dialog汇总
- marsAndroid学习——常见控件DatePicker、DatePickerDialog和TimePicker、TimePickerDialog