]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/filebrowsebutton.py
1 from wxPython
.wx
import *
4 #----------------------------------------------------------------------
6 class FileBrowseButton(wxPanel
):
7 ''' A control to allow the user to type in a filename
8 or browse with the standard file dialog to select file
11 parent, id, pos, size -- passed directly to wxPanel initialisation
12 style = wxTAB_TRAVERSAL -- passed directly to wxPanel initialisation
13 labelText -- Text for label to left of text field
14 buttonText -- Text for button which launches the file dialog
16 dialogTitle -- Title used in file dialog
17 startDirectory -- Default directory for file dialog startup
18 fileMask -- File mask (glob pattern, such as *.*) to use in file dialog
19 fileMode -- wxOPEN or wxSAVE, indicates type of file dialog to use
20 changeCallback -- callback receives all changes in value of control
22 GetValue() -- retrieve current value of text control
23 SetValue(string) -- set current value of text control
24 label -- pointer to internal label widget
25 textControl -- pointer to internal text control
26 browseButton -- pointer to button
28 def __init__ (self
, parent
, id= -1,
29 pos
= wxDefaultPosition
, size
= wxDefaultSize
,
30 style
= wxTAB_TRAVERSAL
,
31 labelText
= "File Entry:",
33 toolTip
= "Type filename or browse computer to choose file",
34 # following are the values for a file dialog box
35 dialogTitle
= "Choose a file",
40 # callback for when value changes (optional)
43 wxPanel
.__init
__ (self
, parent
, id, pos
, size
, style
)
45 self
.dialogTitle
= dialogTitle
46 self
.startDirectory
= startDirectory
47 self
.fileMask
= fileMask
48 self
.fileMode
= fileMode
49 self
.changeCallback
= changeCallback
51 box
= wxBoxSizer(wxHORIZONTAL
)
52 self
.label
= wxStaticText(self
, -1, labelText
, style
=wxALIGN_RIGHT
)
53 font
= self
.label
.GetFont()
54 w
, h
, d
, e
= self
.GetFullTextExtent(labelText
, font
)
55 self
.label
.SetSize(wxSize(w
+5, h
))
56 box
.Add( self
.label
, 0, wxCENTER
)
59 self
.textControl
= wxTextCtrl(self
, ID
)
60 self
.textControl
.SetToolTipString( toolTip
)
61 box
.Add( self
.textControl
, 1, wxLEFT|wxCENTER
, 5)
63 EVT_TEXT(self
.textControl
, ID
, changeCallback
)
66 self
.browseButton
= button
=wxButton(self
, ID
, buttonText
)
67 box
.Add( button
, 0, wxCENTER
)
68 button
.SetToolTipString( toolTip
)
69 EVT_BUTTON(button
, ID
, self
.OnBrowse
)
71 # add a border around the whole thing and resize the panel to fit
72 outsidebox
= wxBoxSizer(wxVERTICAL
)
73 outsidebox
.Add(box
, 1, wxEXPAND|wxALL
, 3)
76 self
.SetAutoLayout(true
)
77 self
.SetSizer( outsidebox
)
79 if size
.width
!= -1 or size
.height
!= -1:
84 def OnBrowse (self
, event
= None):
85 ''' Going to browse for file... '''
86 current
= self
.GetValue ()
87 directory
= os
.path
.split(current
)
88 if os
.path
.isdir( current
):
90 elif directory
and os
.path
.isdir( directory
[0] ):
91 directory
= directory
[0]
93 directory
= self
.startDirectory
94 dlg
= wxFileDialog(self
, self
.dialogTitle
, directory
, current
, self
.fileMask
, self
.fileMode
)
95 if dlg
.ShowModal() == wxID_OK
:
96 self
.SetValue (dlg
.GetPath())
98 self
.textControl
.SetFocus()
102 ''' Convenient access to text control value '''
103 return self
.textControl
.GetValue ()
106 def SetValue (self
, value
):
107 ''' Convenient setting of text control value '''
108 return self
.textControl
.SetValue (value
)
111 #----------------------------------------------------------------------
114 if __name__
== "__main__":
115 #from skeletonbuilder import rulesfile
116 class DemoFrame( wxFrame
):
117 def __init__(self
, parent
):
118 wxFrame
.__init
__(self
, parent
, 2400, "File entry with browse", size
=(500,100) )
119 panel
= wxPanel (self
,-1)
120 innerbox
= wxBoxSizer(wxVERTICAL
)
121 control
= FileBrowseButton(panel
)
122 innerbox
.Add( control
, 0, wxEXPAND
)
123 control
= FileBrowseButton(panel
, labelText
= "With Callback", style
= wxSUNKEN_BORDER
,changeCallback
= self
.OnFileNameChanged
)
124 innerbox
.Add( control
, 0, wxEXPAND|wxALIGN_BOTTOM
)
125 panel
.SetAutoLayout(true
)
126 panel
.SetSizer( innerbox
)
128 def OnFileNameChanged (self
, event
):
129 print "Filename changed", event
.GetString ()
131 def OnCloseMe(self
, event
):
134 def OnCloseWindow(self
, event
):
137 class DemoApp(wxApp
):
139 wxImage_AddHandler(wxJPEGHandler())
140 wxImage_AddHandler(wxPNGHandler())
141 wxImage_AddHandler(wxGIFHandler())
142 frame
= DemoFrame(NULL
)
143 #frame = RulesPanel(NULL )
145 self
.SetTopWindow(frame
)
151 print 'Creating dialogue'