This is probably a better topic for the hackmy scripting section, but I'm going to post it here first!
I typically use zenity for GUI dialogs with my bash scripts, but some times it just doesn't do what I want it to do (..a good tutorial on zenity
can be found here). Because of the limited nature of zenity dialogs, I've been trying to figure out how to use gtkDialog / Glade with bash scripts. There does not seem to be much good documentation of this, but after a bit of trial and error, I have a simple working example to share!
To do this from a standard shell/bash script we need a minimum of two files.. the main script, and a functions file that contains all bash function called by user actions in the dialog (ie. a button press);
To use less than these two files, you must call gtkdialog and not bash or shell as the script type on the first line of the script.
...see edit belowHere is my working example.. create all files in the same working directory, make the shell script executable, then start it from Konsole:
#! /bin/bash
##########################################################################
##########################################################################
##########################################################################
function mk_yesno(){
#USAGE: mk_yesno $"Question text?" "yes-button-icon" "yes-button-function" "no-button-icon" "no-button-function"
export MAIN_DIALOG='
<window title=" - Update-Notifier - " resizable="false" icon-name="computer">
<vbox>
<text use-markup="true" width-chars="40">
<label>"'`echo $1`'"</label>
</text>
<hbox>
<button>
<label>"'`gettext " No "`'"</label>
<input file icon="'`echo $4`'"></input>
<action>'`echo $5`'</action>
<action type="exit">no button clicked</action>
</button>
<button>
<label>"'`gettext " Yes "`'"</label>
<input file icon="'`echo $2`'"></input>
<action>'`echo $3`'</action>
<action type="exit">yes button clicked</action>
</button>
</hbox>
</vbox>
</window>
'
}
# If using glade to create gtk dialogs, call gtkdialog as follows:
## gtkdialog --center --glade-xml=test.glade --program=yes_no --include=test.functions
# Note that --program=<dialog-id> calls the dialog ID as identified in the glade file.
# ..because a glade file can have multiple dialogs defined
mk_yesno $"Would you like update-notifier to give pop-up status notifications?" "help-faq" 'print_this "yes button clicked"' "stop" 'print_this "no button clicked"'
gtkdialog --center --include=test.functions --program=MAIN_DIALOG
mk_yesno $"Would you like to talk about linux?" "irc_section" 'print_this "yes button clicked"' "gtk-quit" 'print_this "no button clicked"'
gtkdialog --center --include=test.functions --program=MAIN_DIALOG
##########################################################################
##########################################################################
##########################################################################
sleep 2
exit
..note that use of the mk_yesno function is not required, the MAIN_DIALOG could be defined independently, but the way I did it allows it to be reused with different text, icons, and functions!

#! /bin/bash
function print_this() {
echo "print_this function says: $1"
}
..anybody want a
gui interface like these for their bash scripts?
EDIT:Here is a method of using gtkdialog with bash that does not require the use of a .functions file.. instead the stdout of the gtkdialog is captured and evaluated to bash variables:
#! /bin/bash
##! /usr/bin/gtkdialog -e
##########################################################################
##########################################################################
##########################################################################
function mk_yesno(){
#USAGE: mk_yesno $"Question text?" "yes-button-icon" "no-button-icon"
export MAIN_DIALOG='
<window title=" - Update-Notifier - " resizable="false" icon-name="computer">
<vbox>
<text use-markup="true" width-chars="40">
<label>"'`echo $1`'"</label>
</text>
<hbox>
<button>
<label>"'`gettext " No "`'"</label>
<input file icon="'`echo $3`'"></input>
<action type="exit">no button clicked</action>
</button>
<button>
<label>"'`gettext " Yes "`'"</label>
<input file icon="'`echo $2`'"></input>
<action type="exit">yes button clicked</action>
</button>
</hbox>
</vbox>
</window>
'
}
function parse_dialog_stdout() {
I=$IFS; IFS=""
for STATEMENT in $(gtkdialog --center --program=MAIN_DIALOG); do
eval $STATEMENT
done
IFS=$I
}
mk_yesno $"Would you like to talk about linux?" "irc_section" "gtk-quit"
parse_dialog_stdout
if [ "$EXIT" ]; then echo -e "\n\nExit action: $EXIT\n"; fi
mk_yesno $"Would you like update-notifier to give pop-up status notifications?" "help-faq" "stop"
parse_dialog_stdout
if [ "$EXIT" ]; then echo -e "\n\nExit action: $EXIT\n"; fi
##########################################################################
##########################################################################
##########################################################################
sleep 1
exit
Resources:
http://forum.goblinx.com.br/viewtopic.php?p=3436 ..a great introduction!
http://www.murga-linux.com/puppy/viewtopic.php?t=38608http://www.murga-linux.com/puppy/viewtopic.php?t=40418GTKDialog Examples:
http://xpt.sourceforge.net/techdocs/language/gtkdialog/gtkde02-GtkdialogExamples/GTKDialog Manual:
http://xpt.sourceforge.net/techdocs/language/gtkdialog/gtkde03-GtkdialogUserManual/Glade tutorials:
http://www.kplug.org/glade_tutorial/glade1_tutorial/gladekplugtut.html ..I also have a pdf tutorial that is too big to post

..and one more great tutorial to add, from our very own PCLinuxOS magazine (by musonio):
http://pclosmag.com/html/Issues/200910/page21.html