# This file provides a generic Directory browser selection widget.
# AUTHOR: MontaVista Software, Inc. <source@mvista.com>
# Copyright 2001 MontaVista Software Inc.
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
class DirBrowserDialog(Pmw
.MegaToplevel
):
def __init__(self
, parent
= None, **kw
):
# Define the megawidget options.
('hidedotfiles', 1, INITOPT
),
('label', None, INITOPT
),
#('labelmargin', 0, INITOPT),
#('labelpos', None, INITOPT),
('borderx', 20, INITOPT
),
('bordery', 20, INITOPT
),
self
.defineoptions(kw
, optiondefs
)
# Initialise the base class (after defining the options).
Pmw
.MegaToplevel
.__init
__(self
, parent
)
interior
= self
.interior()
self
.childframe
= self
.createcomponent('childframe', (), None,
self
.childframe
.pack(expand
= 1,
self
.labelframe
= self
.createcomponent('labelframe', (), None,
self
.labelframe
.pack(padx
= 10, pady
= 10, expand
= 1, fill
= 'both')
self
.label
= self
.createcomponent('label', (), None,
self
.label
.place(x
= (10 + self
['borderx']), y
= 10, anchor
= 'w')
self
.workframe
= self
.createcomponent('workframe', (), None,
self
.workframe
.pack(padx
= self
['borderx'],
self
.buttonframe
= self
.createcomponent('buttonframe', (), None,
self
.buttonframe
.pack(expand
= 0,
self
.optbox
= self
.createcomponent('optbox', (), None,
self
.optbox
.bind('<Configure>', self
._setMinimumSize
)
self
.listbox
= self
.createcomponent('listbox', (), None,
dblclickcommand
= self
._select
,
self
.entry
= self
.createcomponent('entryfield', (), None,
command
= self
.enteredpath
,
label_text
= 'Current Path:',
#self.createlabel(self.workframe, childCols = 1, childRows = 3)
self
.buttonbox
= self
.createcomponent('buttonbox', (), None,
self
.buttonbox
.add('OK', text
= 'OK',
self
.buttonbox
.add('Cancel', text
= 'Cancel',
command
= self
.cancelbutton
)
self
.buttonbox
.add('New Directory', text
= 'New Directory',
command
= self
.newdirbutton
)
self
.buttonbox
.alignbuttons()
self
.buttonbox
.pack(expand
= 1, fill
= 'x')
self
.optbox
.grid(row
= 2, column
= 2, sticky
= 'ew')
self
.listbox
.grid(row
= 3, column
= 2, sticky
= 'news')
self
.entry
.grid(row
= 5, column
= 2, sticky
= 'ew')
self
.workframe
.grid_rowconfigure(3, weight
= 1)
self
.workframe
.grid_rowconfigure(4, minsize
= 20)
self
.workframe
.grid_columnconfigure(2, weight
= 1)
self
.setpath(self
['path'])
# Check keywords and initialise options.
path
= os
.path
.abspath(os
.path
.expanduser(path
))
path
= os
.path
.dirname(path
)
hidedotfiles
= self
['hidedotfiles']
posix
= (os
.name
== 'posix')
for entry
in os
.listdir(path
):
entryPath
= path
+ '/' + entry
if hidedotfiles
and entry
[0] == '.':
# skip dot files if desired
if not os
.path
.isdir(entryPath
):
if not os
.access(entryPath
, os
.R_OK | os
.X_OK
):
# skip directories we can't enter any way
self
.entry
.setentry(self
['path'])
self
.entry
.setentry(path
)
self
.listbox
.setlist(dirlist
)
path
= os
.path
.dirname(path
)
self
.optbox
.setitems(pathlist
, 0)
def _setMinimumSize(self
, event
):
# If the optionmenu changes width, make sure it does not
owidth
= self
.optbox
.winfo_width()
self
.workframe
.grid_columnconfigure(2, minsize
= owidth
)
sel
= self
.listbox
.getcurselection()
self
.setpath(os
.path
.dirname(self
['path']))
self
.setpath(self
['path'] + '/' + sel
[0])
self
.setpath(self
.entry
.get())
self
.deactivate(self
['path'])
CreateDirectoryPopup(self
.interior(), self
['path'])
self
.setpath(self
['path'])
class CreateDirectoryPopup
:
def __init__(self
, parent
, path
):
self
.newdirpopup
= Pmw
.PromptDialog(parent
,
buttons
= ('OK', 'Cancel'),
entryfield_labelpos
= 'nw',
label_text
= 'Enter new directory name for:\n%s'%self
.path
,
command
= self
._buttonpress
self
.newdirpopup
.activate()
def _buttonpress(self
, button
):
newdirname
= self
.newdirpopup
.get()
dirlist
= os
.listdir(self
.path
)
if newdirname
in dirlist
:
'Error: "%s", already exists as a file or directory.'%newdirname
)
os
.mkdir(self
.path
+ '/' + newdirname
)
'Error: Could not create directory: "%s"'%newdirname
)
self
.newdirpopup
.deactivate()
self
.newdirpopup
.deactivate()
def ErrorPopup(parent
, message
):
error
= Pmw
.MessageDialog(parent
, title
= 'Error',
if __name__
== '__main__':
rootWin
.title('Directory Browser Dialog Demo')
# Create the hierarchical directory browser widget
dirBrowserDialog
= DirBrowserDialog(rootWin
,
label
= 'Select a directory',
title
= 'Directory Selector',
dir = dirBrowserDialog
.activate()
print 'Selected Directory:', dir
dirButton
= Tkinter
.Button(rootWin
, text
="Browser", command
=buildBrowser
)
dirButton
.pack(side
= 'left', padx
= 10, pady
= 10)
exitButton
= Tkinter
.Button(rootWin
, text
="Quit", command
=rootWin
.quit
)
exitButton
.pack(side
= 'left', padx
= 10, pady
= 10)