Principle
Prev Glade2script Reference Manual Next



Principle

The interface will be created using glade-3. Anotehr script, called as the glade file will be associated with it. Available languages for this script are Bash and Python, or any other you may want to use.
In this script, two types of actions may be inserted :

glade2script, written in Python, will be used as an interface between the script and the GTK interface. It is directly connected to the stdout of the main script (listening to each echo command). Once commands are received, glade2script translate them and send the corresponding command to GTK.

In order to send information to GTK using the script, command should be written in a FIFO file and sent to the script. glade2script, being connected to this file, retrieves datas and translate them into function calls or variables loading into the environment.

In order to retrieve the value of a widget, 2 solutions are available :

# glade2script and pygtk command
echo 'SET@_label1.set_text(“my text”)'
echo 'GET@_entry1.get_text()'

# glade2script command
echo "TREE@@LOAD@@treeview1@@/tmp/liste_tree"
echo "IMG@@_img_tux@@tux.png@@150@@150"
recuperation de variablefonction


The 2 main ways to use glade2script :

­Using the static interface

In the main script, the GUI is called and the variables from the widgets passed as arguments are retrieved (just as Zenity would do it for example)
#/bin/bash
SORTIE_GUI=$(glade2script -g ./my_file.glade -r '_entry1.get_text')
echo “$SORTIE_GUI”
#Result:
_entry1_get_text=”text written  in input”
EXIT=”ok”

­Using the dynamic interface :

Actions are performed as a reaction to the interface. The corresponding code should be placed in the script associated to the glade file. The interface should not be closed in order to retrieve the output data. To start the engine, a simple script should be created to start glade2script.

Both static and dynamic interfaces may be used at the same time, depending on your needs.



Simple example :

Here is the context : I want to create a simple dialog box, containing one question and two buttons (accept and decline) using my script (using the static interface).
One should first of all create the dialog box in glade.
The script with glade, having the same filename.
#! /bin/bash
PID=$$
FIFO=/tmp/FIFO${PID}
mkfifo $FIFO
function btn_ok()
{
echo 'EXIT@@SAVE'
}
function btn_no()
{
echo 'EXIT@@'
}
# The nested loop is the core of communication between GTK and Bash
# It should always be placed at the very end of the assiciated file.
# You may not need it, but it is possible to catch signals and use them in a broader way than simply calling functions and load variables.
while read ligne; do
      if [[ "$ligne" =~ GET@ ]]; then
           eval ${ligne#*@}
           echo "DEBUG => in boucle bash :" ${ligne#*@}
      else
           echo "DEBUG=> in bash NOT GET" $ligne
           $ligne
     fi
done < <(while true; do
      read entree < /tmp/FIFO
      [[ "$entree" == "QuitNow" ]] && break
        echo $entree
done)
Your script:
#! /bin/bash
# 2 ways of retrieving the final result : using the output code or in loading the EXIT environment variable and handle the result by yourself.
# First method, with the output code
if glade2script -g ./mon_gui.glade; then
   echo “the ok button was clicked”
else
   echo “the no button was clicked”
fi
#
#
# second metyhod, using the EXIT variable
GUI=$(glade2script -g ./mon_gui.glade)
eval “${GUI}”
If [[ “${EXIT}” == “yes” ]]; then
   echo “the ok button was clicked”
else
   echo “the no button was clicked”
fi
# Depending on the user choice
# the variable EXIT can be  EXIT=”yes” or EXIT=”no”,
# and the output code put to 0 or 1


Safety measures :

General:

You may use the set -f command at the beginning of your script, and set +f at the end to solve the avoid any problem.

Glade:


Gtk Widgets :


glade2script commands:


Associated script:

exec 4>$(tty)
echo 'something that won't be interpreted by Python' >&4
#close the fd at the end of the script.
exec 4<&-

NOTE :

The PID of glade2script can be accessed via the command GET@glade2script_PID
The variables G2S_SCREEN_HEIGHT, G2S_SCREEN_WIDTH, G2S_TERMINAL_PID are added in the current environment.



Prev Glade2script Reference Manual Next