| |
- Dialog
- exceptions.Exception(exceptions.BaseException)
-
- error
-
- BadPythonDialogUsage
- DialogError
- DialogTerminatedBySignal
- ExecutableNotFound
- ProbablyPythonBug
- PythonDialogBug
- PythonDialogSystemError
-
- PythonDialogErrorBeforeExecInChildProcess
- PythonDialogIOError
- PythonDialogOSError
- PythonDialogReModuleError
- UnableToCreateTemporaryDirectory
- UnexpectedDialogOutput
class Dialog |
|
Class providing bindings for dialog-compatible programs.
This class allows you to invoke dialog or a compatible program in
a pythonic way to build quicky and easily simple but nice text
interfaces.
An application typically creates one instance of the Dialog class
and uses it for all its widgets, but it is possible to use
concurrently several instances of this class with different
parameters (such as the background title) if you have the need
for this.
The exit code (exit status) returned by dialog is to be
compared with the DIALOG_OK, DIALOG_CANCEL, DIALOG_ESC,
DIALOG_ERROR, DIALOG_EXTRA and DIALOG_HELP attributes of the
Dialog instance (they are integers).
Note: although this class does all it can to allow the caller to
differentiate between the various reasons that caused a
dialog box to be closed, its backend, dialog 0.9a-20020309a
for my tests, doesn't always return DIALOG_ESC when the
user presses the ESC key, but often returns DIALOG_ERROR
instead. The exit codes returned by the corresponding
Dialog methods are of course just as wrong in these cases.
You've been warned.
Public methods of the Dialog class (mainly widgets)
---------------------------------------------------
The Dialog class has the following methods:
add_persistent_args
calendar
checklist
fselect
gauge_start
gauge_update
gauge_stop
infobox
inputbox
menu
msgbox
passwordbox
radiolist
scrollbox
tailbox
textbox
timebox
yesno
clear (obsolete)
setBackgroundTitle (obsolete)
Passing dialog "Common Options"
-------------------------------
Every widget method has a **kwargs argument allowing you to pass
dialog so-called Common Options (see the dialog(1) manual page)
to dialog for this widget call. For instance, if `d' is a Dialog
instance, you can write:
d.checklist(args, ..., title="A Great Title", no_shadow=1)
The no_shadow option is worth looking at:
1. It is an option that takes no argument as far as dialog is
concerned (unlike the "--title" option, for instance). When
you list it as a keyword argument, the option is really
passed to dialog only if the value you gave it evaluates to
true, e.g. "no_shadow=1" will cause "--no-shadow" to be
passed to dialog whereas "no_shadow=0" will cause this
option not to be passed to dialog at all.
2. It is an option that has a hyphen (-) in its name, which you
must change into an underscore (_) to pass it as a Python
keyword argument. Therefore, "--no-shadow" is passed by
giving a "no_shadow=1" keyword argument to a Dialog method
(the leading two dashes are also consistently removed).
Exceptions
----------
Please refer to the specific methods' docstrings or simply to the
module's docstring for a list of all exceptions that might be
raised by this class' methods. |
|
Methods defined here:
- __init__(self, dialog='dialog', DIALOGRC=None, compat='dialog', use_stdout=None)
- Constructor for Dialog instances.
dialog -- name of (or path to) the dialog-like program to
use; if it contains a '/', it is assumed to be a
path and is used as is; otherwise, it is looked
for according to the contents of the PATH
environment variable, which defaults to
":/bin:/usr/bin" if unset.
DIALOGRC -- string to pass to the dialog-like program as the
DIALOGRC environment variable, or None if no
modification to the environment regarding this
variable should be done in the call to the
dialog-like program
compat -- compatibility mode (see below)
The officially supported dialog-like program in pythondialog
is the well-known dialog program written in C, based on the
ncurses library. It is also known as cdialog and its home
page is currently (2004-03-15) located at:
http://dickey.his.com/dialog/dialog.html
If you want to use a different program such as Xdialog, you
should indicate the executable file name with the `dialog'
argument *and* the compatibility type that you think it
conforms to with the `compat' argument. Currently, `compat'
can be either "dialog" (for dialog; this is the default) or
"Xdialog" (for, well, Xdialog).
The `compat' argument allows me to cope with minor
differences in behaviour between the various programs
implementing the dialog interface (not the text or graphical
interface, I mean the "API"). However, having to support
various APIs simultaneously is a bit ugly and I would really
prefer you to report bugs to the relevant maintainers when
you find incompatibilities with dialog. This is for the
benefit of pretty much everyone that relies on the dialog
interface.
Notable exceptions:
ExecutableNotFound
PythonDialogOSError
- add_persistent_args(self, arglist)
- calendar(self, text, height=6, width=0, day=0, month=0, year=0, **kwargs)
- Display a calendar dialog box.
text -- text to display in the box
height -- height of the box (minus the calendar height)
width -- width of the box
day -- inititial day highlighted
month -- inititial month displayed
year -- inititial year selected (0 causes the current date
to be used as the initial date)
A calendar box displays month, day and year in separately
adjustable windows. If the values for day, month or year are
missing or negative, the current date's corresponding values
are used. You can increment or decrement any of those using
the left, up, right and down arrows. Use tab or backtab to
move between windows. If the year is given as zero, the
current date is used as an initial value.
Return a tuple of the form (code, date) where `code' is the
exit status (an integer) of the dialog-like program and
`date' is a list of the form [day, month, year] (where `day',
`month' and `year' are integers corresponding to the date
chosen by the user) if the box was closed with OK, or None if
it was closed with the Cancel button.
Notable exceptions:
- any exception raised by self._perform()
- UnexpectedDialogOutput
- PythonDialogReModuleError
- checklist(self, text, height=15, width=54, list_height=7, choices=[], **kwargs)
- Display a checklist box.
text -- text to display in the box
height -- height of the box
width -- width of the box
list_height -- number of entries displayed in the box (which
can be scrolled) at a given time
choices -- a list of tuples (tag, item, status) where
`status' specifies the initial on/off state of
each entry; can be 0 or 1 (integers, 1 meaning
checked, i.e. "on"), or "on", "off" or any
uppercase variant of these two strings.
Return a tuple of the form (code, [tag, ...]) with the tags
for the entries that were selected by the user. `code' is the
exit status of the dialog-like program.
If the user exits with ESC or CANCEL, the returned tag list
is empty.
Notable exceptions:
any exception raised by self._perform() or _to_onoff()
- clear(self)
- Clear the screen. Equivalent to the dialog --clear option.
This method is obsolete. Please remove calls to it from your
programs.
- fselect(self, filepath, height, width, **kwargs)
- Display a file selection dialog box.
filepath -- initial file path
height -- height of the box
width -- width of the box
The file-selection dialog displays a text-entry window in
which you can type a filename (or directory), and above that
two windows with directory names and filenames.
Here, filepath can be a file path in which case the file and
directory windows will display the contents of the path and
the text-entry window will contain the preselected filename.
Use tab or arrow keys to move between the windows. Within the
directory or filename windows, use the up/down arrow keys to
scroll the current selection. Use the space-bar to copy the
current selection into the text-entry window.
Typing any printable character switches focus to the
text-entry window, entering that character as well as
scrolling the directory and filename windows to the closest
match.
Use a carriage return or the "OK" button to accept the
current value in the text-entry window, or the "Cancel"
button to cancel.
Return a tuple of the form (code, path) where `code' is the
exit status (an integer) of the dialog-like program and
`path' is the path chosen by the user (whose last element may
be a directory or a file).
Notable exceptions:
any exception raised by self._perform()
- gauge_iterate = gauge_update(self, percent, text='', update_text=0)
- gauge_start(self, text='', height=8, width=54, percent=0, **kwargs)
- Display gauge box.
text -- text to display in the box
height -- height of the box
width -- width of the box
percent -- initial percentage shown in the meter
A gauge box displays a meter along the bottom of the box. The
meter indicates a percentage.
This function starts the dialog-like program telling it to
display a gauge box with a text in it and an initial
percentage in the meter.
Return value: undefined.
Gauge typical usage
-------------------
Gauge typical usage (assuming that `d' is an instance of the
Dialog class) looks like this:
d.gauge_start()
# do something
d.gauge_update(10) # 10% of the whole task is done
# ...
d.gauge_update(100, "any text here") # work is done
exit_code = d.gauge_stop() # cleanup actions
Notable exceptions:
- any exception raised by self._call_program()
- PythonDialogOSError
- gauge_stop(self)
- Terminate a running gauge.
This function performs the appropriate cleanup actions to
terminate a running gauge (started with `gauge_start').
See the `gauge_start' function's documentation for
information about how to use a gauge.
Return value: undefined.
Notable exceptions:
- any exception raised by
self._wait_for_program_termination()
- PythonDialogIOError can be raised if closing the pipe
used to talk to the dialog-like program fails.
- gauge_update(self, percent, text='', update_text=0)
- Update a running gauge box.
percent -- new percentage to show in the gauge meter
text -- new text to optionally display in the box
update-text -- boolean indicating whether to update the
text in the box
This function updates the percentage shown by the meter of a
running gauge box (meaning `gauge_start' must have been
called previously). If update_text is true (for instance, 1),
the text displayed in the box is also updated.
See the `gauge_start' function's documentation for
information about how to use a gauge.
Return value: undefined.
Notable exception: PythonDialogIOError can be raised if there
is an I/O error while writing to the pipe
used to talk to the dialog-like program.
- infobox(self, text, height=10, width=30, **kwargs)
- Display an information dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
An info box is basically a message box. However, in this
case, dialog will exit immediately after displaying the
message to the user. The screen is not cleared when dialog
exits, so that the message will remain on the screen until
the calling shell script clears it later. This is useful
when you want to inform the user that some operations are
carrying on that may require some time to finish.
Return the exit status (an integer) of the dialog-like
program.
Notable exceptions:
any exception raised by self._perform()
- inputbox(self, text, height=10, width=30, init='', **kwargs)
- Display an input dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
init -- default input string
An input box is useful when you want to ask questions that
require the user to input a string as the answer. If init is
supplied it is used to initialize the input string. When
entering the string, the BACKSPACE key can be used to
correct typing errors. If the input string is longer than
can fit in the dialog box, the input field will be scrolled.
Return a tuple of the form (code, string) where `code' is the
exit status of the dialog-like program and `string' is the
string entered by the user.
Notable exceptions:
any exception raised by self._perform()
- menu(self, text, height=15, width=54, menu_height=7, choices=[], **kwargs)
- Display a menu dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
menu_height -- number of entries displayed in the box (which
can be scrolled) at a given time
choices -- a sequence of (tag, item) or (tag, item, help)
tuples (the meaning of each `tag', `item' and
`help' is explained below)
Overview
--------
As its name suggests, a menu box is a dialog box that can be
used to present a list of choices in the form of a menu for
the user to choose. Choices are displayed in the order given.
Each menu entry consists of a `tag' string and an `item'
string. The tag gives the entry a name to distinguish it from
the other entries in the menu. The item is a short
description of the option that the entry represents.
The user can move between the menu entries by pressing the
UP/DOWN keys, the first letter of the tag as a hot-key, or
the number keys 1-9. There are menu-height entries displayed
in the menu at one time, but the menu will be scrolled if
there are more entries than that.
Providing on-line help facilities
---------------------------------
If this function is called with item_help=1 (keyword
argument), the option --item-help is passed to dialog and the
tuples contained in `choices' must contain 3 elements each :
(tag, item, help). The help string for the highlighted item
is displayed in the bottom line of the screen and updated as
the user highlights other items.
If item_help=0 or if this keyword argument is not passed to
this function, the tuples contained in `choices' must contain
2 elements each : (tag, item).
If this function is called with help_button=1, it must also
be called with item_help=1 (this is a limitation of dialog),
therefore the tuples contained in `choices' must contain 3
elements each as explained in the previous paragraphs. This
will cause a Help button to be added to the right of the
Cancel button (by passing --help-button to dialog).
Return value
------------
Return a tuple of the form (exit_info, string).
`exit_info' is either:
- an integer, being the the exit status of the dialog-like
program
- or the string "help", meaning that help_button=1 was
passed and that the user chose the Help button instead of
OK or Cancel.
The meaning of `string' depends on the value of exit_info:
- if `exit_info' is 0, `string' is the tag chosen by the
user
- if `exit_info' is "help", `string' is the `help' string
from the `choices' argument corresponding to the item
that was highlighted when the user chose the Help button
- otherwise (the user chose Cancel or pressed Esc, or there
was a dialog error), the value of `string' is undefined.
Notable exceptions:
any exception raised by self._perform()
- msgbox(self, text, height=10, width=30, **kwargs)
- Display a message dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
A message box is very similar to a yes/no box. The only
difference between a message box and a yes/no box is that a
message box has only a single OK button. You can use this
dialog box to display any message you like. After reading
the message, the user can press the ENTER key so that dialog
will exit and the calling program can continue its
operation.
Return the exit status (an integer) of the dialog-like
program.
Notable exceptions:
any exception raised by self._perform()
- passwordbox(self, text, height=10, width=60, init='', **kwargs)
- Display an password input dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
init -- default input password
A password box is similar to an input box, except that the
text the user enters is not displayed. This is useful when
prompting for passwords or other sensitive information. Be
aware that if anything is passed in "init", it will be
visible in the system's process table to casual snoopers.
Also, it is very confusing to the user to provide them with a
default password they cannot see. For these reasons, using
"init" is highly discouraged.
Return a tuple of the form (code, password) where `code' is
the exit status of the dialog-like program and `password' is
the password entered by the user.
Notable exceptions:
any exception raised by self._perform()
- radiolist(self, text, height=15, width=54, list_height=7, choices=[], **kwargs)
- Display a radiolist box.
text -- text to display in the box
height -- height of the box
width -- width of the box
list_height -- number of entries displayed in the box (which
can be scrolled) at a given time
choices -- a list of tuples (tag, item, status) where
`status' specifies the initial on/off state
each entry; can be 0 or 1 (integers, 1 meaning
checked, i.e. "on"), or "on", "off" or any
uppercase variant of these two strings.
No more than one entry should be set to on.
A radiolist box is similar to a menu box. The main difference
is that you can indicate which entry is initially selected,
by setting its status to on.
Return a tuple of the form (code, tag) with the tag for the
entry that was chosen by the user. `code' is the exit status
of the dialog-like program.
If the user exits with ESC or CANCEL, or if all entries were
initially set to off and not altered before the user chose
OK, the returned tag is the empty string.
Notable exceptions:
any exception raised by self._perform() or _to_onoff()
- scrollbox(self, text, height=20, width=78, **kwargs)
- Display a string in a scrollable box.
text -- text to display in the box
height -- height of the box
width -- width of the box
This method is a layer on top of textbox. The textbox option
in dialog allows to display file contents only. This method
allows you to display any text in a scrollable box. This is
simply done by creating a temporary file, calling textbox and
deleting the temporary file afterwards.
Return the dialog-like program's exit status.
Notable exceptions:
- UnableToCreateTemporaryDirectory
- PythonDialogIOError
- PythonDialogOSError
- exceptions raised by the tempfile module (which are
unfortunately not mentioned in its documentation, at
least in Python 2.3.3...)
- setBackgroundTitle(self, text)
- Set the background title for dialog.
This method is obsolete. Please remove calls to it from your
programs.
- tailbox(self, filename, height=20, width=60, **kwargs)
- Display the contents of a file in a dialog box, as in "tail -f".
filename -- name of the file whose contents is to be
displayed in the box
height -- height of the box
width -- width of the box
Display the contents of the specified file, updating the
dialog box whenever the file grows, as with the "tail -f"
command.
Return the exit status (an integer) of the dialog-like
program.
Notable exceptions:
any exception raised by self._perform()
- textbox(self, filename, height=20, width=60, **kwargs)
- Display the contents of a file in a dialog box.
filename -- name of the file whose contents is to be
displayed in the box
height -- height of the box
width -- width of the box
A text box lets you display the contents of a text file in a
dialog box. It is like a simple text file viewer. The user
can move through the file by using the UP/DOWN, PGUP/PGDN
and HOME/END keys available on most keyboards. If the lines
are too long to be displayed in the box, the LEFT/RIGHT keys
can be used to scroll the text region horizontally. For more
convenience, forward and backward searching functions are
also provided.
Return the exit status (an integer) of the dialog-like
program.
Notable exceptions:
any exception raised by self._perform()
- timebox(self, text, height=3, width=30, hour=-1, minute=-1, second=-1, **kwargs)
- Display a time dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
hour -- inititial hour selected
minute -- inititial minute selected
second -- inititial second selected
A dialog is displayed which allows you to select hour, minute
and second. If the values for hour, minute or second are
negative (or not explicitely provided, as they default to
-1), the current time's corresponding values are used. You
can increment or decrement any of those using the left-, up-,
right- and down-arrows. Use tab or backtab to move between
windows.
Return a tuple of the form (code, time) where `code' is the
exit status (an integer) of the dialog-like program and
`time' is a list of the form [hour, minute, second] (where
`hour', `minute' and `second' are integers corresponding to
the time chosen by the user) if the box was closed with OK,
or None if it was closed with the Cancel button.
Notable exceptions:
- any exception raised by self._perform()
- PythonDialogReModuleError
- UnexpectedDialogOutput
- yesno(self, text, height=10, width=30, **kwargs)
- Display a yes/no dialog box.
text -- text to display in the box
height -- height of the box
width -- width of the box
A yes/no dialog box of size `height' rows by `width' columns
will be displayed. The string specified by `text' is
displayed inside the dialog box. If this string is too long
to fit in one line, it will be automatically divided into
multiple lines at appropriate places. The text string can
also contain the sub-string "\n" or newline characters to
control line breaking explicitly. This dialog box is useful
for asking questions that require the user to answer either
yes or no. The dialog box has a Yes button and a No button,
in which the user can switch between by pressing the TAB
key.
Return the exit status (an integer) of the dialog-like
program.
Notable exceptions:
any exception raised by self._perform()
|
|