Wx. filedialog for wxpython Control Learning

Source: Internet
Author: User

1. wx. filedialog allows you to select one or more files from the system files. Supports wildcard characters to allow users to select files of interest. For example, "BMP files (*. BMP) | *. BMP | GIF files (*. GIF) | *. GIF" only displays and selects the image suffix type BMP and GIF.

2. Window styles

Wx. fd_open Single file selection dialog box
Wx. fd_save File Save dialog box
Wx. fd_overwrite_prompt Only valid for the SAVE dialog box. When the file is overwritten, a prompt dialog box is displayed.
Wx. fd_multiple Only valid for opening the dialog box. Multiple options are supported.
Wx. fd_change_dir Change the current working directory to the selected folder

3. method set:

 

Method Summary
Filedialog

__init__(self, parent, message, defaultDir, defaultFile, wildcard, style, pos) 
Parent: parent window. Messale: The title displayed in the file dialog box. defadir DIR is the default folder and defafile file is the default file. wildcard matches the wildcard and style = Window style.

String GetDirectory(self)Returns the default folder.
String GetFilename(self)Get the default file name
Pyobject GetFilenames(self)Returns the list of selected files.
Int GetFilterIndex(self) 
Returns the index into the list of filters supplied, optionally, In the wildcard parameter.
String GetMessage(self)Return file dialog box title
String GetPath(self)Returns the full path of the selected file (including the file path and file name)
Pyobject GetPaths(self)Returns the full path list of the selected files.
String GetWildcard(self)Returns a wildcard.
  SetDirectory(self, dir)Set the default file directory
  SetFilename(self, name)Set the default file name
  SetFilterIndex(self, filterIndex) 
Sets the default filter index, starting from zero.
  SetMessage(self, message)Set the dialog box title
  SetPath(self, path)Set the default full path name of the selected file
  SetWildcard(self, wildCard)Design wildcard characters

4. Example:

A. Create an app main portal Program

#-*-Coding: UTF-8-*-# handler # Name: module shell # purpose: main entry function of the application # Author: ankier # created: 28-10-2012 # copyright: (c) ankier 2012 # licence: <your licence> # kernel import wxfrom mainframe import mainframeimport sys ##@ detail shellapp core class shellapp (wx. APP): def oninit (Self): mainframe = mainframe () mainframe. show (true) return true # @ detail main portal program if _ name _ = '_ main _': APP = shellapp () # redirect wxpython output input and error output to System Standard Input and Output sys. stdin = sys. _ stdin _ sys. stdout = sys. _ stdout _ sys. stderr = sys. _ stderr _ app. mainloop ()

B. Use of the mainframe file selection dialog box

#-*-Coding: UTF-8-*-# modules # Name: module mainframe # purpose: main interface of the application # Author: ankier # created: 28-10-2012 # copyright: (c) ankier 2012 # licence: <your licence> # emerge import wximport OS ##@ detail mainframe main interface window 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 "selected file") 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. A Ll | wx. align_centre) self. _ textbox = textbox self. setsizerandfit (Sizer) def _ buildmenus (Self): mainmenubar = wx. menubar () filemenu = wx. menu () filemenuitem = filemenu. append (-1, "open a single file") self. BIND (wx. evt_menu, self. _ opensinglefile, filemenuitem) savemenuitem = filemenu. append (-1, "save file") self. BIND (wx. evt_menu, self. _ SaveFile, savemenuitem) savepromptmenuitem = filemenu. append (-1, "saving files and prompting overwriting ") Self. BIND (wx. evt_menu, self. _ savepromptfile, savepromptmenuitem) multipleopenmenuitem = filemenu. append (-1, "Multi-file selection") self. BIND (wx. evt_menu, self. _ multipleselectfiles, multipleopenmenuitem) mainmenubar. append (filemenu, Title = U' & file ') 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 = "select a single file", 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 = "save file", 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 = "Save & prompt file", wildcard = filesfilter, style = wx. fd_save | wx. fd_overwrite_prompt) dialogresult = filedialog. showmodal () I F 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 = "Multi-file selection", wildcard = filesfilter, style = wx. fd_open | wx. fd_multiple) dialogresult = filediple. showmodal () If dialogresult! = Wx. id_ OK: Return paths = filedialog. getpaths () self. _ textbox. setlabel ('') for Path in paths: Self. _ textbox. appendtext (path + '\ n ')

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.