Re: bash vs. python scripts - which one is better?
On Sun, Aug 19, 2007 at 11:03:50PM -0500, Anthony M Simonelli wrote:
> On Sun, 2007-08-19 at 14:35 -0500, Ron Johnson wrote:
>
> > Python + Tcl/Tk should be easier than Python + Gtk.
> >
> > Even simpler would be bash + dialog or it's GUI companion gtkdialog.
> >
>
> How about Zenity? I've used it before to provide a GUI interface to
> some of my simple bash scripts.
Here's my samples from my notes directory. There's one for curses and
one that does a couple of ways of using dialog, the latter way uses a
dialog module I wrote and include at the end. The module takes care of
packing the strings that go to the dialog command line making the main
program line clean and neat.
Disclaimer: I've only used this a couple of times since most of my
stuff uses normal text i/o.
Doug.
------
curses:
#!/usr/bin/env python
# from p 416 in python 2.1 bible.
import curses
try:
MainWindow=curses.initscr()
MainWindow.addstr(1,3, "Hello", curses.A_BOLD | curses.A_UNDERLINE )
MainWindow.refresh()
curses.echo()
MainWindow.getch()
finally:
curses.endwin()
Dialog:
#!/usr/bin/python -O
""" Tests the Dialog examples, in python, using different ways """
# Import statements
import os
import dialog
# "Constants"
DIALOG=" /usr/bin/dialog "
# Definition of classes and functions
# Main Function
if (__name__=="__main__"):
# message box
Dialog_title=' --title "Message Box"'
Dialog_options=' --clear'
Dialog_box_options=' --msgbox'
Dialog_text=' "Hi, this is a simple message box. You can use this to'
Dialog_text+=' display any message you like. The box will remain until'
Dialog_text+=' you press the ENTER key."'
Dialog_height=' 10'
Dialog_width=' 41'
Dialog_CMD=DIALOG + Dialog_title + Dialog_options + Dialog_box_options + Dialog_text
Dialog_CMD+=Dialog_height + Dialog_width
os.system(Dialog_CMD)
#input box
Dialog_CMD=DIALOG + ' --title "InputBox" --clear --inputbox \
"Hi, this is an input dialog box. You can use this to ask questions \
that require the user to input a string as the answer. You can \
input strings longer than the width of the input box, in that case, the \
input field will be automatially scrolled. You can use BACKSPACE to \
correct errors.\
\
Try entering your name below:" \
16 51'
#rc=os.system(Dialog_CMD)
# rc is 0 since dialog exited OK. we need to see stderr
#use os.popen (or if necesary popen4)
w,rc=os.popen4(Dialog_CMD)
del w
print rc.read()
del rc
## file selector doesn't work right
## now try dialog module
hello=dialog.Dialog()
# message box
hello.title='--title "Message Box" '
hello.boxtype='--msgbox '
hello.text='"Message box using dialog class" '
hello.height='10 '
hello.width='41 '
w,r=os.popen4(hello.show())
print r.read()
Here's the dialog module:
#dialog.py
"""
Module wrapper for /usr/bin/dialog. See man page.
Options for dialog are attributes of a dialog instance.
e.g. instead of adding --title, do:
obj.title=' --title "This is the title" '
Remember to include a trailing space in each attribute
method pack() returns the complete dialog to run
using:
Dialog_CMD=dialog.show()
w,r = os.popen4(Dialog_CMD)
This allows access to stdin and stdout/stderr. Guage bars, for
example, read from stdin until get EOF.
"""
class Dialog:
"""Object version of Dialog: attributes same as Dialog's
options"""
def __init__(self):
self.DIALOG = " /usr/bin/dialog "
# common options
self.aspect=''
self.backtitle=''
self.beep=''
self.beepafter=''
self.begin=''
self.cancellabel=''
self.clear=''
self.crwrap=''
self.defaultno=''
self.defaultitem=''
self.helpbutton=''
self.helplabel=''
self.itemhelp=''
self.maxinput=''
self.nokill=''
self.nocancel=''
self.noshadow=''
self.oklabel=''
self.printmaxsize=''
self.printsize=''
self.separateoutput=''
self.separatewidget=''
self.shadow=''
self.sleep=''
self.stdout=''
self.tabcorrect=''
self.tablen=''
self.timeout=''
self.title=''
self.trim=''
# box type and box options
#this is only the type, e.g. obj.box_type='infobox'
self.boxtype=''
self.text=''
self.height=''
self.width=''
self.boxoptions=''
#this is other box options specific to the type of box, placed
# after text, height, and width
def show(self):
self.Dialog_CMD=self.DIALOG + self.aspect + self.backtitle\
+ self.beep + self.beepafter + self.begin \
+ self.cancellabel + self.clear + self.crwrap \
+ self.defaultno + self.defaultitem + self.helpbutton\
+ self.helplabel + self.itemhelp\
+ self.maxinput + self.nokill + self.nocancel \
+ self.nocancel + self.noshadow + self.oklabel \
+ self.printmaxsize + self.printsize \
+ self.separateoutput + self.separatewidget \
+ self.shadow + self.sleep + self.stdout + self.tabcorrect \
+ self.tablen + self.timeout + self.title + self.trim \
+ self.boxtype + self.text + self.height + self.width \
+ self.boxoptions
return(self.Dialog_CMD)
Reply to: