Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 357 lines (288 sloc) 12.1 KB
#! /usr/bin/env python
# --- A simple demonstration program for pythondialog
# Copyright (C) 2000 Robb Shecter, Sultanbek Tezadov
# Copyright (C) 2002, 2004 Florent Rougon
# This program is in the public domain.
"""Demonstration program for pythondialog.
This is a simple program demonstrating the possibilities offered by
the pythondialog module (which is itself a Python interface to the
well-known dialog utility, or any other program compatible with
Please have a look at the documentation for the `handle_exit_code'
function in order to understand the somewhat relaxed error checking
policy for pythondialog calls in this demo.
import sys, os, os.path, time, string, dialog
# XXX We should handle the new DIALOG_HELP and DIALOG_EXTRA return codes here.
def handle_exit_code(d, code):
"""Sample function showing how to interpret the dialog exit codes.
This function is not used after every call to dialog in this demo
for two reasons:
1. For some boxes, unfortunately, dialog returns the code for
ERROR when the user presses ESC (instead of the one chosen
for ESC). As these boxes only have an OK button, and an
exception is raised and correctly handled here in case of
real dialog errors, there is no point in testing the dialog
exit status (it can't be CANCEL as there is no CANCEL
button; it can't be ESC as unfortunately, the dialog makes
it appear as an error; it can't be ERROR as this is handled
in to raise an exception; therefore, it *is* OK).
2. To not clutter simple code with things that are
demonstrated elsewhere.
# d is supposed to be a Dialog instance
if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
if code == d.DIALOG_CANCEL:
msg = "You chose cancel in the last dialog box. Do you want to " \
"exit this demo?"
msg = "You pressed ESC in the last dialog box. Do you want to " \
"exit this demo?"
# "No" or "ESC" will bring the user back to the demo.
# DIALOG_ERROR is propagated as an exception and caught in main().
# So we only need to handle OK here.
if d.yesno(msg) == d.DIALOG_OK:
return 0
return 1 # code is d.DIALOG_OK
def infobox_demo(d):
# Exit code thrown away to keey this demo code simple (however, real
# errors are propagated by an exception)
d.infobox("One moment, please. Just wasting some time here to "
"show you the infobox...")
def gauge_demo(d):
d.gauge_start("Progress: 0%", title="Still testing your patience...")
for i in range(1, 101):
if i < 50:
d.gauge_update(i, "Progress: %d%%" % i, update_text=1)
elif i == 50:
d.gauge_update(i, "Over %d%%. Good." % i, update_text=1)
elif i == 80:
d.gauge_update(i, "Yeah, this boring crap will be over Really "
"Soon Now.", update_text=1)
def yesno_demo(d):
# Return the answer given to the question (also specifies if ESC was
# pressed)
return d.yesno("Do you like this demo?")
def msgbox_demo(d, answer):
if answer == d.DIALOG_OK:
d.msgbox("Excellent! Press OK to see the source code.")
d.msgbox("Well, feel free to send your complaints to /dev/null!")
def textbox_demo(d):
d.textbox("", width=76)
def inputbox_demo(d):
# If the user presses Cancel, he is asked (by handle_exit_code) if he
# wants to exit the demo. We loop as long as he tells us he doesn't want
# to do so.
while 1:
(code, answer) = d.inputbox("What's your name?", init="Snow White")
if handle_exit_code(d, code):
return answer
def menu_demo(d):
while 1:
(code, tag) =
"What's your favorite day of the week?",
choices=[("Monday", "Being the first day of the week..."),
("Tuesday", "Comes after Monday"),
("Wednesday", "Before Thursday day"),
("Thursday", "Itself after Wednesday"),
("Friday", "The best day of all"),
("Saturday", "Well, I've had enough, thanks"),
("Sunday", "Let's rest a little bit")])
if handle_exit_code(d, code):
return tag
def checklist_demo(d):
while 1:
# We could put non-empty items here (not only the tag for each entry)
(code, tag) = d.checklist(text="What sandwich toppings do you like?",
height=15, width=54, list_height=7,
choices=[("Catsup", "", 0),
("Mustard", "", 0),
("Pesto", "", 0),
("Mayonaise", "", 1),
("Horse radish","", 1),
("Sun-dried tomatoes", "", 1)],
title="Do you prefer ham or spam?",
backtitle="And now, for something "
"completely different...")
if handle_exit_code(d, code):
return tag
def form_demo(d):
while 1:
(code, tag) = d.form(text="Which are your favourites?",
height=15, width=54, form_height=7,
fields=[("Colour", "", 10, 30),
("Rugby Team", "", 20),
("Car", "", 20),
("Celebrity", "", 20)],
title="A demo of the form dialog.",
backtitle="And now, for something "
"completely different...")
if handle_exit_code(d, code):
return tag
def passwordform_demo(d):
while 1:
(code, tag) = d.passwordform(text="Set your password",
height=15, width=54, form_height=7,
fields=[("Admin Password:", "", 10, 20),
(" Confirm:", "", 10, 20)],
title="A demo of the passwordform dialog.",
backtitle="And now, for something "
"completely different...")
if handle_exit_code(d, code):
return tag
def radiolist_demo(d):
while 1:
(code, tag) = d.radiolist(
"What's your favorite kind of sandwich?",
choices=[("Hamburger", "2 slices of bread, a steak...", 0),
("Hotdog", "doesn't bite any more", 0),
("Burrito", "no se lo que es", 0),
("Doener", "Huh?", 0),
("Falafel", "Erm...", 0),
("Bagel", "Of course!", 0),
("Big Mac", "Ah, that's easy!", 1),
("Whopper", "Erm, sorry", 0),
("Quarter Pounder", 'called "le Big Mac" in France', 0),
("Peanut Butter and Jelly", "Well, that's your own "
"business...", 0),
("Grilled cheese", "And nothing more?", 0)])
if handle_exit_code(d, code):
return tag
def calendar_demo(d):
while 1:
(code, date) = d.calendar("When do you think Debian sarge will be "
"released?", year=0)
if handle_exit_code(d, code):
return date
def passwordbox_demo(d):
while 1:
(code, password) = d.passwordbox("What is your root password, "
"so that I can crack your system "
"right now?")
if handle_exit_code(d, code):
return password
def comment_on_sarge_release_date(day, month, year):
if year < 2004 or (year == 2004 and month <= 3):
return "Mmm... what about a little tour on"
elif year == 2004 and month <= 4:
return """\
Damn, how optimistic! You don't know much about Debian, do you?"""
elif year == 2004 and month <= 7:
return """\
Well, good guess. But who knows what the future reserves to us? ;-)"""
elif year == 2004:
return """\
Oh, well. That's plausible. But please, please don't depress
other people with your pronostics... ;-)"""
return "Hey, you're a troll! (or do you know Debian *so* well? ;-)"
def scrollbox_demo(d, name, favorite_day, toppings, favourites, passwords,
sandwich, date, password):
day, month, year = date
msg = """\
Here are some vital statistics about you:
Name: %s
Favorite day of the week: %s
Favorite sandwich toppings:%s
Favorite sandwich: %s
You estimate Debian sarge's release to happen around %04u-%02u-%02u.
Your root password is: ************************** (looks good!)""" \
% (name, favorite_day,
string.join([''] + toppings, "\n "),
string.join([] + favourites, ", "),
string.join([] + passwords, ", confirmed "),
sandwich, year, month, day,
comment_on_sarge_release_date(day, month, year))
d.scrollbox(msg, height=20, width=75, title="Great Report of the Year")
def fselect_demo(d):
while 1:
root_dir = os.sep # This is OK for UNIX systems
dir = os.getenv("HOME", root_dir)
# Make sure the directory we chose ends with os.sep() so that dialog
# shows its contents right away
if dir and dir[-1] != os.sep:
dir = dir + os.sep
(code, path) = d.fselect(dir, 10, 50,
title="Cute little file to show as "
"in a `tail -f'")
if handle_exit_code(d, code):
if not os.path.isfile(path):
d.scrollbox("Hmm. Didn't I ask you to select a *file*?",
width=50, height=10)
return path
def tailbox_demo(d, file):
d.tailbox(file, 20, 60, title="You are brave. You deserve the "
"right to rest, now." )
def demo():
# If you want to use Xdialog (pathnames are also OK for the 'dialog'
# argument)
# d = dialog.Dialog(dialog="Xdialog", compat="Xdialog")
d = dialog.Dialog(dialog="dialog")
d.add_persistent_args(["--backtitle", "pythondialog demo"])
answer = yesno_demo(d)
msgbox_demo(d, answer)
name = inputbox_demo(d)
favorite_day = menu_demo(d)
toppings = checklist_demo(d)
favourites = form_demo(d)
passwords = passwordform_demo(d)
sandwich = radiolist_demo(d)
date = calendar_demo(d)
password = passwordbox_demo(d)
scrollbox_demo(d, name, favorite_day, toppings, favourites, passwords, sandwich, date, password)
Haha. You thought it was over. Wrong. Even More fun is to come!
(well, depending on your definition on "fun")
Now, please select a file you would like to see growing (or not...).""",
file = fselect_demo(d)
tailbox_demo(d, file)
Now, you're done. No, I'm not kidding.
So, why the hell are you sitting here instead of rushing on that EXIT
button? Ah, you did like the demo. Hmm... are you feeling OK? ;-)""",
def main():
"""This demo shows the main features of the pythondialog Dialog class.
except dialog.error, exc_instance:
sys.stderr.write("Error:\n\n%s\n" % exc_instance.complete_message())
if __name__ == "__main__": main()