HomePage > Software Index > Development > gtkdialog > Tips and Tricks

2. Let the script return data

The first step is to show a dialog window on your screen, then the next step is the interaction of the user with the gui. I think the easiest way is to divide guis into different groups. - Simple, Complex and Projects.


Simple
A simple dialog allows the user to give a response to a question/query on the screen:

When the user click on the yes/no/cancel/ok-button the dialog exits and the script continues to evaluate the user input.
Here is an example:

export script='
<vbox>
 <text><label>Are you sure?</label></text>
 <hbox>
  <button no></button>
  <button yes><action>EXIT:sure</action></button>
 </hbox>
</vbox>'

I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog --program=script); do
  eval $STATEMENTS
done
IFS=$I

[ $EXIT = sure ] && gxmessage 'You are sure'


Complex
For more complex dialogs, we want to keep the gui alive even if the user clicks on something, so using an EXIT value is not the right choice. Calling pre-defined functions from inside the gtkdialog gui is a handy way to do this. We must remember to export all functions before executing our gui.

now () {
  date > /tmp/date
}
export -f now

export script='
<vbox>
 <entry>
  <variable>ENTRY_DATE</variable>
  <input>cat /tmp/date</input>
 </entry>
 <button>
  <label>Refresh</label>
  <action>now</action>
  <action>refresh:ENTRY_DATE</action>
 </button>
</vbox>'

gtkdialog -p script


Project
When your project grows and its complexity makes it hard to work with, it's time to consider another approach. Splitting code into several files might be much more usable when working with your code. Group familiar functions into one file... In this example, the function-file must be saved in /root/test to work. Instead of sending <action> to an EXIT-variable, we send it to the file with the function 'now'.

NOTE, that I don't use an ordinary function (like the previous example), but a case command instead. The benefits are faster loading of your app since it does not read your functions into memory, but the greatest advantage is that you can edit the function while the gui is running. The downside are slower execution of the function.

Function file
case $1 in
-now)
  date > /tmp/date
  exit;;
esac

Main file
export script='
<vbox>
 <entry>
  <variable>ENTRY_DATE</variable>
  <input>cat /tmp/date</input>
 </entry>
 <button>
  <label>Refresh</label>
  <action>/root/test -now</action>
  <action>refresh:ENTRY_DATE</action>
 </button>
</vbox>'

I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog -p script); do
  eval $STATEMENTS
done
IFS=$I



Categories
CategoryGtkdialog
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki