xref: /freebsd/contrib/dialog/samples/dialog.py (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
12a3e3873SBaptiste Daroussin#!/usr/bin/python
2*a96ef450SBaptiste Daroussin# $Id: dialog.py,v 1.5 2019/12/10 22:52:52 tom Exp $
34c8945a0SNathan Whitehorn# Module: dialog.py
44c8945a0SNathan Whitehorn# Copyright (c) 2000 Robb Shecter <robb@acm.org>
54c8945a0SNathan Whitehorn# All rights reserved.
64c8945a0SNathan Whitehorn# This source is covered by the GNU GPL.
74c8945a0SNathan Whitehorn#
84c8945a0SNathan Whitehorn# This module is a Python wrapper around the Linux "dialog" utility
94c8945a0SNathan Whitehorn# by Savio Lam and Stuart Herbert.  My goals were to make dialog as
104c8945a0SNathan Whitehorn# easy to use from Python as possible.  The demo code at the end of
114c8945a0SNathan Whitehorn# the module is a good example of how to use it.  To run the demo,
124c8945a0SNathan Whitehorn# execute:
134c8945a0SNathan Whitehorn#
144c8945a0SNathan Whitehorn#                       python dialog.py
154c8945a0SNathan Whitehorn#
164c8945a0SNathan Whitehorn# This module has one class in it, "Dialog".  An application typically
174c8945a0SNathan Whitehorn# creates an instance of it, and possibly sets the background title option.
184c8945a0SNathan Whitehorn# Then, methods can be called on it for interacting with the user.
194c8945a0SNathan Whitehorn#
204c8945a0SNathan Whitehorn# I wrote this because I want to use my 486-33 laptop as my main
214c8945a0SNathan Whitehorn# development computer (!), and I wanted a way to nicely interact with the
224c8945a0SNathan Whitehorn# user in console mode.  There are apparently other modules out there
234c8945a0SNathan Whitehorn# with similar functionality, but they require the Python curses library.
244c8945a0SNathan Whitehorn# Writing this module from scratch was easier than figuring out how to
254c8945a0SNathan Whitehorn# recompile Python with curses enabled. :)
264c8945a0SNathan Whitehorn#
274c8945a0SNathan Whitehorn# One interesting feature is that the menu and selection windows allow
284c8945a0SNathan Whitehorn# *any* objects to be displayed and selected, not just strings.
294c8945a0SNathan Whitehorn#
304c8945a0SNathan Whitehorn# TO DO:
314c8945a0SNathan Whitehorn#   Add code so that the input buffer is flushed before a dialog box is
324c8945a0SNathan Whitehorn#     shown.  This would make the UI more predictable for users.  This
334c8945a0SNathan Whitehorn#     feature could be turned on and off through an instance method.
344c8945a0SNathan Whitehorn#   Drop using temporary files when interacting with 'dialog'
354c8945a0SNathan Whitehorn#     (it's possible -- I've already tried :-).
364c8945a0SNathan Whitehorn#   Try detecting the terminal window size in order to make reasonable
374c8945a0SNathan Whitehorn#     height and width defaults.  Hmmm - should also then check for
384c8945a0SNathan Whitehorn#     terminal resizing...
394c8945a0SNathan Whitehorn#   Put into a package name to make more reusable - reduce the possibility
404c8945a0SNathan Whitehorn#     of name collisions.
414c8945a0SNathan Whitehorn#
424c8945a0SNathan Whitehorn# NOTES:
434c8945a0SNathan Whitehorn#         there is a bug in (at least) Linux-Mandrake 7.0 Russian Edition
444c8945a0SNathan Whitehorn#         running on AMD K6-2 3D that causes core dump when 'dialog'
454c8945a0SNathan Whitehorn#         is running with --gauge option;
464c8945a0SNathan Whitehorn#         in this case you'll have to recompile 'dialog' program.
474c8945a0SNathan Whitehorn#
484c8945a0SNathan Whitehorn# Modifications:
494c8945a0SNathan Whitehorn# Jul 2000, Sultanbek Tezadov (http://sultan.da.ru)
504c8945a0SNathan Whitehorn#    Added:
514c8945a0SNathan Whitehorn#       - 'gauge' widget *)
524c8945a0SNathan Whitehorn#       - 'title' option to some widgets
534c8945a0SNathan Whitehorn#       - 'checked' option to checklist dialog; clicking "Cancel" is now
544c8945a0SNathan Whitehorn#           recognizable
554c8945a0SNathan Whitehorn#       - 'selected' option to radiolist dialog; clicking "Cancel" is now
564c8945a0SNathan Whitehorn#           recognizable
574c8945a0SNathan Whitehorn#       - some other cosmetic changes and improvements
584c8945a0SNathan Whitehorn#
594c8945a0SNathan Whitehorn
604c8945a0SNathan Whitehornimport os
614c8945a0SNathan Whitehornfrom tempfile import mktemp
624c8945a0SNathan Whitehornfrom string import split
634c8945a0SNathan Whitehornfrom time import sleep
644c8945a0SNathan Whitehorn
654c8945a0SNathan Whitehorn#
664c8945a0SNathan Whitehorn# Path of the dialog executable
674c8945a0SNathan Whitehorn#
684c8945a0SNathan WhitehornDIALOG = os.getenv("DIALOG");
694c8945a0SNathan Whitehornif DIALOG is None:
704c8945a0SNathan Whitehorn	DIALOG="../dialog";
714c8945a0SNathan Whitehorn
724c8945a0SNathan Whitehornclass Dialog:
734c8945a0SNathan Whitehorn    def __init__(self):
744c8945a0SNathan Whitehorn	self.__bgTitle = ''               # Default is no background title
754c8945a0SNathan Whitehorn
764c8945a0SNathan Whitehorn
774c8945a0SNathan Whitehorn    def setBackgroundTitle(self, text):
784c8945a0SNathan Whitehorn	self.__bgTitle = '--backtitle "%s"' % text
794c8945a0SNathan Whitehorn
804c8945a0SNathan Whitehorn
814c8945a0SNathan Whitehorn    def __perform(self, cmd):
824c8945a0SNathan Whitehorn	"""Do the actual work of invoking dialog and getting the output."""
834c8945a0SNathan Whitehorn	fName = mktemp()
844c8945a0SNathan Whitehorn	rv = os.system('%s %s %s 2> %s' % (DIALOG, self.__bgTitle, cmd, fName))
854c8945a0SNathan Whitehorn	f = open(fName)
864c8945a0SNathan Whitehorn	output = f.readlines()
874c8945a0SNathan Whitehorn	f.close()
884c8945a0SNathan Whitehorn	os.unlink(fName)
894c8945a0SNathan Whitehorn	return (rv, output)
904c8945a0SNathan Whitehorn
914c8945a0SNathan Whitehorn
924c8945a0SNathan Whitehorn    def __perform_no_options(self, cmd):
934c8945a0SNathan Whitehorn	"""Call dialog w/out passing any more options. Needed by --clear."""
944c8945a0SNathan Whitehorn	return os.system(DIALOG + ' ' + cmd)
954c8945a0SNathan Whitehorn
964c8945a0SNathan Whitehorn
974c8945a0SNathan Whitehorn    def __handleTitle(self, title):
984c8945a0SNathan Whitehorn	if len(title) == 0:
994c8945a0SNathan Whitehorn	    return ''
1004c8945a0SNathan Whitehorn	else:
1014c8945a0SNathan Whitehorn	    return '--title "%s" ' % title
1024c8945a0SNathan Whitehorn
1034c8945a0SNathan Whitehorn
1044c8945a0SNathan Whitehorn    def yesno(self, text, height=10, width=30, title=''):
1054c8945a0SNathan Whitehorn	"""
1064c8945a0SNathan Whitehorn	Put a Yes/No question to the user.
1074c8945a0SNathan Whitehorn	Uses the dialog --yesno option.
1084c8945a0SNathan Whitehorn	Returns a 1 or a 0.
1094c8945a0SNathan Whitehorn	"""
1104c8945a0SNathan Whitehorn	(code, output) = self.__perform(self.__handleTitle(title) +\
1114c8945a0SNathan Whitehorn	    '--yesno "%s" %d %d' % (text, height, width))
1124c8945a0SNathan Whitehorn	return code == 0
1134c8945a0SNathan Whitehorn
1144c8945a0SNathan Whitehorn
1154c8945a0SNathan Whitehorn    def msgbox(self, text, height=10, width=30, title=''):
1164c8945a0SNathan Whitehorn	"""
1174c8945a0SNathan Whitehorn	Pop up a message to the user which has to be clicked
1184c8945a0SNathan Whitehorn	away with "ok".
1194c8945a0SNathan Whitehorn	"""
1204c8945a0SNathan Whitehorn	self.__perform(self.__handleTitle(title) +\
1214c8945a0SNathan Whitehorn	    '--msgbox "%s" %d %d' % (text, height, width))
1224c8945a0SNathan Whitehorn
1234c8945a0SNathan Whitehorn
1244c8945a0SNathan Whitehorn    def infobox(self, text, height=10, width=30):
1254c8945a0SNathan Whitehorn	"""Make a message to the user, and return immediately."""
1264c8945a0SNathan Whitehorn	self.__perform('--infobox "%s" %d %d' % (text, height, width))
1274c8945a0SNathan Whitehorn
1284c8945a0SNathan Whitehorn
1294c8945a0SNathan Whitehorn    def inputbox(self, text, height=10, width=30, init='', title=''):
1304c8945a0SNathan Whitehorn	"""
1314c8945a0SNathan Whitehorn	Request a line of input from the user.
1324c8945a0SNathan Whitehorn	Returns the user's input or None if cancel was chosen.
1334c8945a0SNathan Whitehorn	"""
1344c8945a0SNathan Whitehorn	(c, o) = self.__perform(self.__handleTitle(title) +\
1354c8945a0SNathan Whitehorn	    '--inputbox "%s" %d %d "%s"' % (text, height, width, init))
1364c8945a0SNathan Whitehorn	try:
1374c8945a0SNathan Whitehorn	    return o[0]
1384c8945a0SNathan Whitehorn	except IndexError:
1394c8945a0SNathan Whitehorn	    if c == 0:  # empty string entered
1404c8945a0SNathan Whitehorn		return ''
1414c8945a0SNathan Whitehorn	    else:  # canceled
1424c8945a0SNathan Whitehorn		return None
1434c8945a0SNathan Whitehorn
1444c8945a0SNathan Whitehorn
1454c8945a0SNathan Whitehorn    def textbox(self, filename, height=20, width=60, title=None):
1464c8945a0SNathan Whitehorn	"""Display a file in a scrolling text box."""
1474c8945a0SNathan Whitehorn	if title is None:
1484c8945a0SNathan Whitehorn	    title = filename
1494c8945a0SNathan Whitehorn	self.__perform(self.__handleTitle(title) +\
1504c8945a0SNathan Whitehorn	    ' --textbox "%s" %d %d' % (filename, height, width))
1514c8945a0SNathan Whitehorn
1524c8945a0SNathan Whitehorn
1534c8945a0SNathan Whitehorn    def menu(self, text, height=15, width=54, list=[]):
1544c8945a0SNathan Whitehorn	"""
1554c8945a0SNathan Whitehorn	Display a menu of options to the user.  This method simplifies the
1564c8945a0SNathan Whitehorn	--menu option of dialog, which allows for complex arguments.  This
1574c8945a0SNathan Whitehorn	method receives a simple list of objects, and each one is assigned
1584c8945a0SNathan Whitehorn	a choice number.
1594c8945a0SNathan Whitehorn	The selected object is returned, or None if the dialog was canceled.
1604c8945a0SNathan Whitehorn	"""
1614c8945a0SNathan Whitehorn	menuheight = height - 8
1624c8945a0SNathan Whitehorn	pairs = map(lambda i, item: (i + 1, item), range(len(list)), list)
1634c8945a0SNathan Whitehorn	choices = reduce(lambda res, pair: res + '%d "%s" ' % pair, pairs, '')
1644c8945a0SNathan Whitehorn	(code, output) = self.__perform('--menu "%s" %d %d %d %s' %\
1654c8945a0SNathan Whitehorn	    (text, height, width, menuheight, choices))
1664c8945a0SNathan Whitehorn	try:
1674c8945a0SNathan Whitehorn	    return list[int(output[0]) - 1]
1684c8945a0SNathan Whitehorn	except IndexError:
1694c8945a0SNathan Whitehorn	    return None
1704c8945a0SNathan Whitehorn
1714c8945a0SNathan Whitehorn
1724c8945a0SNathan Whitehorn    def checklist(self, text, height=15, width=54, list=[], checked=None):
1734c8945a0SNathan Whitehorn	"""
1744c8945a0SNathan Whitehorn	Returns a list of the selected objects.
1754c8945a0SNathan Whitehorn	Returns an empty list if nothing was selected.
1764c8945a0SNathan Whitehorn	Returns None if the window was canceled.
1774c8945a0SNathan Whitehorn	checked -- a list of boolean (0/1) values; len(checked) must equal
1784c8945a0SNathan Whitehorn	    len(list).
1794c8945a0SNathan Whitehorn	"""
1804c8945a0SNathan Whitehorn	if checked is None:
1814c8945a0SNathan Whitehorn	    checked = [0]*len(list)
1824c8945a0SNathan Whitehorn	menuheight = height - 8
1834c8945a0SNathan Whitehorn	triples = map(
1844c8945a0SNathan Whitehorn	    lambda i, item, onoff, fs=('off', 'on'): (i + 1, item, fs[onoff]),
1854c8945a0SNathan Whitehorn	    range(len(list)), list, checked)
1864c8945a0SNathan Whitehorn	choices = reduce(lambda res, triple: res + '%d "%s" %s ' % triple,
1874c8945a0SNathan Whitehorn	    triples, '')
1884c8945a0SNathan Whitehorn	(c, o) = self.__perform('--checklist "%s" %d %d %d %s' %\
1894c8945a0SNathan Whitehorn	    (text, height, width, menuheight, choices))
1904c8945a0SNathan Whitehorn	try:
1914c8945a0SNathan Whitehorn	    output = o[0]
1924c8945a0SNathan Whitehorn	    indexList  = map(lambda x: int(x[1:-1]), split(output))
1934c8945a0SNathan Whitehorn	    objectList = filter(lambda item, list=list, indexList=indexList:
1944c8945a0SNathan Whitehorn		    list.index(item) + 1 in indexList,
1954c8945a0SNathan Whitehorn		list)
1964c8945a0SNathan Whitehorn	    return objectList
1974c8945a0SNathan Whitehorn	except IndexError:
1984c8945a0SNathan Whitehorn	    if c == 0:                        # Nothing was selected
1994c8945a0SNathan Whitehorn		return []
2004c8945a0SNathan Whitehorn	    return None  # Was canceled
2014c8945a0SNathan Whitehorn
2024c8945a0SNathan Whitehorn
2034c8945a0SNathan Whitehorn    def radiolist(self, text, height=15, width=54, list=[], selected=0):
2044c8945a0SNathan Whitehorn	"""
2054c8945a0SNathan Whitehorn	Return the selected object.
2064c8945a0SNathan Whitehorn	Returns empty string if no choice was selected.
2074c8945a0SNathan Whitehorn	Returns None if window was canceled.
2084c8945a0SNathan Whitehorn	selected -- the selected item (must be between 1 and len(list)
2094c8945a0SNathan Whitehorn	    or 0, meaning no selection).
2104c8945a0SNathan Whitehorn	"""
2114c8945a0SNathan Whitehorn	menuheight = height - 8
2124c8945a0SNathan Whitehorn	triples = map(lambda i, item: (i + 1, item, 'off'),
2134c8945a0SNathan Whitehorn	    range(len(list)), list)
2144c8945a0SNathan Whitehorn	if selected:
2154c8945a0SNathan Whitehorn	    i, item, tmp = triples[selected - 1]
2164c8945a0SNathan Whitehorn	    triples[selected - 1] = (i, item, 'on')
2174c8945a0SNathan Whitehorn	choices = reduce(lambda res, triple: res + '%d "%s" %s ' % triple,
2184c8945a0SNathan Whitehorn	    triples, '')
2194c8945a0SNathan Whitehorn	(c, o) = self.__perform('--radiolist "%s" %d %d %d %s' %\
2204c8945a0SNathan Whitehorn	    (text, height, width, menuheight, choices))
2214c8945a0SNathan Whitehorn	try:
2224c8945a0SNathan Whitehorn	    return list[int(o[0]) - 1]
2234c8945a0SNathan Whitehorn	except IndexError:
2244c8945a0SNathan Whitehorn	    if c == 0:
2254c8945a0SNathan Whitehorn		return ''
2264c8945a0SNathan Whitehorn	    return None
2274c8945a0SNathan Whitehorn
2284c8945a0SNathan Whitehorn
2294c8945a0SNathan Whitehorn    def clear(self):
2304c8945a0SNathan Whitehorn	"""
2314c8945a0SNathan Whitehorn	Clear the screen. Equivalent to the dialog --clear option.
2324c8945a0SNathan Whitehorn	"""
2334c8945a0SNathan Whitehorn	self.__perform_no_options('--clear')
2344c8945a0SNathan Whitehorn
2354c8945a0SNathan Whitehorn
2364c8945a0SNathan Whitehorn    def scrollbox(self, text, height=20, width=60, title=''):
2374c8945a0SNathan Whitehorn	"""
2384c8945a0SNathan Whitehorn	This is a bonus method.  The dialog package only has a function to
2394c8945a0SNathan Whitehorn	display a file in a scrolling text field.  This method allows any
2404c8945a0SNathan Whitehorn	string to be displayed by first saving it in a temp file, and calling
2414c8945a0SNathan Whitehorn	--textbox.
2424c8945a0SNathan Whitehorn	"""
2434c8945a0SNathan Whitehorn	fName = mktemp()
2444c8945a0SNathan Whitehorn	f = open(fName, 'w')
2454c8945a0SNathan Whitehorn	f.write(text)
2464c8945a0SNathan Whitehorn	f.close()
2474c8945a0SNathan Whitehorn	self.__perform(self.__handleTitle(title) +\
2484c8945a0SNathan Whitehorn	    '--textbox "%s" %d %d' % (fName, height, width))
2494c8945a0SNathan Whitehorn	os.unlink(fName)
2504c8945a0SNathan Whitehorn
2514c8945a0SNathan Whitehorn
2524c8945a0SNathan Whitehorn    def gauge_start(self, perc=0, text='', height=8, width=54, title=''):
2534c8945a0SNathan Whitehorn	"""
2544c8945a0SNathan Whitehorn	Display gauge output window.
255*a96ef450SBaptiste Daroussin	Gauge normal usage (assuming that there is an instance of 'Dialog'
2564c8945a0SNathan Whitehorn	class named 'd'):
2574c8945a0SNathan Whitehorn	    d.gauge_start()
2584c8945a0SNathan Whitehorn	    # do something
259*a96ef450SBaptiste Daroussin	    d.gauge_iterate(10)  # passed through 10%
2604c8945a0SNathan Whitehorn	    # ...
2614c8945a0SNathan Whitehorn	    d.gauge_iterate(100, 'any text here')  # work is done
2624c8945a0SNathan Whitehorn	    d.stop_gauge()  # clean-up actions
2634c8945a0SNathan Whitehorn	"""
2644c8945a0SNathan Whitehorn	cmd = self.__handleTitle(title) +\
2654c8945a0SNathan Whitehorn	    '--gauge "%s" %d %d %d' % (text, height, width, perc)
2664c8945a0SNathan Whitehorn	cmd = '%s %s %s 2> /dev/null' % (DIALOG, self.__bgTitle, cmd)
2674c8945a0SNathan Whitehorn	self.pipe = os.popen(cmd, 'w')
2684c8945a0SNathan Whitehorn    #/gauge_start()
2694c8945a0SNathan Whitehorn
2704c8945a0SNathan Whitehorn
2714c8945a0SNathan Whitehorn    def gauge_iterate(self, perc, text=''):
2724c8945a0SNathan Whitehorn	"""
2734c8945a0SNathan Whitehorn	Update percentage point value.
2744c8945a0SNathan Whitehorn
2754c8945a0SNathan Whitehorn	See gauge_start() function above for the usage.
2764c8945a0SNathan Whitehorn	"""
2774c8945a0SNathan Whitehorn	if text:
2784c8945a0SNathan Whitehorn	    text = 'XXX\n%d\n%s\nXXX\n' % (perc, text)
2794c8945a0SNathan Whitehorn	else:
2804c8945a0SNathan Whitehorn	    text = '%d\n' % perc
2814c8945a0SNathan Whitehorn	self.pipe.write(text)
2824c8945a0SNathan Whitehorn	self.pipe.flush()
2834c8945a0SNathan Whitehorn    #/gauge_iterate()
2844c8945a0SNathan Whitehorn
2854c8945a0SNathan Whitehorn
2864c8945a0SNathan Whitehorn    def gauge_stop(self):
2874c8945a0SNathan Whitehorn	"""
2884c8945a0SNathan Whitehorn	Finish previously started gauge.
2894c8945a0SNathan Whitehorn
2904c8945a0SNathan Whitehorn	See gauge_start() function above for the usage.
2914c8945a0SNathan Whitehorn	"""
2924c8945a0SNathan Whitehorn	self.pipe.close()
2934c8945a0SNathan Whitehorn    #/gauge_stop()
2944c8945a0SNathan Whitehorn
2954c8945a0SNathan Whitehorn
2964c8945a0SNathan Whitehorn
2974c8945a0SNathan Whitehorn#
2984c8945a0SNathan Whitehorn# DEMO APPLICATION
2994c8945a0SNathan Whitehorn#
3004c8945a0SNathan Whitehornif __name__ == '__main__':
3014c8945a0SNathan Whitehorn    """
3024c8945a0SNathan Whitehorn    This demo tests all the features of the class.
3034c8945a0SNathan Whitehorn    """
3044c8945a0SNathan Whitehorn    d = Dialog()
3054c8945a0SNathan Whitehorn    d.setBackgroundTitle('dialog.py demo')
3064c8945a0SNathan Whitehorn
3074c8945a0SNathan Whitehorn    d.infobox(
3084c8945a0SNathan Whitehorn	"One moment... Just wasting some time here to test the infobox...")
3094c8945a0SNathan Whitehorn    sleep(3)
3104c8945a0SNathan Whitehorn
3114c8945a0SNathan Whitehorn    if d.yesno("Do you like this demo?"):
3124c8945a0SNathan Whitehorn	d.msgbox("Excellent!  Here's the source code:")
3134c8945a0SNathan Whitehorn    else:
3144c8945a0SNathan Whitehorn	d.msgbox("Send your complaints to /dev/null")
3154c8945a0SNathan Whitehorn
3164c8945a0SNathan Whitehorn    d.textbox("dialog.py")
3174c8945a0SNathan Whitehorn
3184c8945a0SNathan Whitehorn    name = d.inputbox("What's your name?", init="Snow White")
3194c8945a0SNathan Whitehorn    fday = d.menu("What's your favorite day of the week?",
3204c8945a0SNathan Whitehorn	list=["Monday", "Tuesday", "Wednesday", "Thursday",
3214c8945a0SNathan Whitehorn	    "Friday (The best day of all)", "Saturday", "Sunday"])
3224c8945a0SNathan Whitehorn    food = d.checklist("What sandwich toppings do you like?",
3234c8945a0SNathan Whitehorn	list=["Catsup", "Mustard", "Pesto", "Mayonaise", "Horse radish",
3244c8945a0SNathan Whitehorn	    "Sun-dried tomatoes"], checked=[0,0,0,1,1,1])
3254c8945a0SNathan Whitehorn    sand = d.radiolist("What's your favorite kind of sandwich?",
3264c8945a0SNathan Whitehorn	list=["Hamburger", "Hotdog", "Burrito", "Doener", "Falafel",
3274c8945a0SNathan Whitehorn	    "Bagel", "Big Mac", "Whopper", "Quarter Pounder",
3284c8945a0SNathan Whitehorn	    "Peanut Butter and Jelly", "Grilled cheese"], selected=4)
3294c8945a0SNathan Whitehorn
3304c8945a0SNathan Whitehorn    # Prepare the message for the final window
3314c8945a0SNathan Whitehorn    bigMessage = "Here are some vital statistics about you:\n\nName: " + name +\
3324c8945a0SNathan Whitehorn        "\nFavorite day of the week: " + fday +\
3334c8945a0SNathan Whitehorn	"\nFavorite sandwich toppings:\n"
3344c8945a0SNathan Whitehorn    for topping in food:
3354c8945a0SNathan Whitehorn	bigMessage = bigMessage + "    " + topping + "\n"
3364c8945a0SNathan Whitehorn    bigMessage = bigMessage + "Favorite sandwich: " + str(sand)
3374c8945a0SNathan Whitehorn
3384c8945a0SNathan Whitehorn    d.scrollbox(bigMessage)
3394c8945a0SNathan Whitehorn
3404c8945a0SNathan Whitehorn    #<>#  Gauge Demo
3414c8945a0SNathan Whitehorn    d.gauge_start(0, 'percentage: 0', title='Gauge Demo')
3424c8945a0SNathan Whitehorn    for i in range(1, 101):
3434c8945a0SNathan Whitehorn	if i < 50:
3444c8945a0SNathan Whitehorn	    msg = 'percentage: %d' % i
3454c8945a0SNathan Whitehorn	elif i == 50:
3464c8945a0SNathan Whitehorn	    msg = 'Over 50%'
3474c8945a0SNathan Whitehorn	else:
3484c8945a0SNathan Whitehorn	    msg = ''
3494c8945a0SNathan Whitehorn	d.gauge_iterate(i, msg)
3504c8945a0SNathan Whitehorn	sleep(0.1)
3514c8945a0SNathan Whitehorn    d.gauge_stop()
3524c8945a0SNathan Whitehorn    #<>#
3534c8945a0SNathan Whitehorn
3544c8945a0SNathan Whitehorn    d.clear()
355