Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/dir_selection_menu.py
Дата изменения: Fri Feb 28 14:46:09 2014
Дата индексирования: Sat Mar 1 21:57:08 2014
Кодировка:
#MODULE dir_selection_menu
#
#***********************************************************************
"""
**PURPOSE** --
Build a widget for selecting a directory.

**DEVELOPER** --
Don Chance

**MODIFICATION HISTORY** --
o Initial implementation 7/24/03
o updated to grab/release focus. dc 10/6/03
o when browse is selected, start at the Browse_dir. dc 4/25/07
"""
#***********************************************************************
import Tix
from Tkconstants import *
from boolean import *

from Tkinter import *

import os
import tkMessageBox
import Pmw

__version__ = "4/25/07"

class dir_selection_menu:
"""A GUI for selecting a directory.
"""
def __init__ (self,
parent,
comment,
Browse_Opt=false,
Browse_dir="",
must_exist_flag=true):
""" Create a Tix Label Entry widget with an optional browse button.
The entry widget is only editable if there is a browse button.
"""
# Create storage for the browse directory if one not given.
self.val = StringVar()

Browse_dir = os.path.abspath(Browse_dir)

self.set_entry(Browse_dir, must_exist_flag)

# Create a new frame for the File Selection Menu.
self.frame = Frame(parent)

Dir_entry = Tix.LabelEntry(self.frame, label=comment, labelside='left')

self.configure = Dir_entry.configure

# Make a pointer to the entry widget to ease configuration.
# Default is not to be able to edit this entry.
self.entry = Dir_entry.entry
if TclVersion >= 8.4:
self.entry.configure(textvariable=self.val,
borderwidth=3,
relief='sunken',
background='snow',
width=58,
state="disable",
disabledbackground='white')
else:
self.entry.configure(textvariable=self.val,
borderwidth=3,
relief='sunken',
background='snow',
width=58,
state="disable")

# Erase the entry widget when Control-u entered.
self.entry.bind ('', self.clear)

Dir_entry.pack(side='left')

# Check if the user can browse another directory for an entry.
if Browse_Opt:

# Allow the entry to be editable.
self.entry.configure(width=47, state= "normal")

# Create a directory browse dialog box.
self.dialog = Tix.DirSelectDialog('')
self.dialog.config(command=self.set_entry)

self.dialog.dirbox.dirlist.config(command=self.set_entry)
if os.path.isdir(Browse_dir):
self.dialog.dirbox.dirlist.config(value=Browse_dir)

self.dialog.ok.config(command=self.select_dir_cmd)
self.dialog.cancel.config(command=self.cancel)

Browse_btn = Button(self.frame,
text='Browse ...',
command=self.create)
Browse_btn.pack(side='left')

def create(self):
self.dialog.popup()
Pmw.pushgrab(self.dialog, 0, None)

def clear(self, event=None):
"""Clear the input file.
"""
self.val.set('')

def get_entry(self):
"""Return the selected file.
"""
return self.val.get()

def set_entry(self, dir, must_exist=true):
"""Set the directory in the entry box.

If the 'must_exist' flag is true, the gui will produce an error
box if the input directory does not exist.
"""
if not dir:
self.clear()
elif not must_exist:
self.val.set(dir)
elif os.path.isdir(dir) and must_exist:
self.val.set(dir)
else:
tkMessageBox.showerror("No such directory", "Directory %s not found" % dir)

def select_dir_cmd(self):
self.val.set(self.dialog.dirbox.cget('value'))
self.dialog.popdown()
Pmw.popgrab(self.dialog)

def cancel(self):
self.dialog.popdown()
Pmw.popgrab(self.dialog)

if __name__ == '__main__':
root = Tix.Tk()
menu = dir_selection_menu(root, "A directory:", true, '/data/scheduling/spss_flight_data/pass')
menu.frame.pack(padx=10, pady=10)
menu1 = dir_selection_menu(root, "Another directory:")
menu1.frame.pack(padx=10, pady=10)
root.mainloop()