Difference between revisions of "How to Use Zenity"

From CDOT Wiki
Jump to: navigation, search
 
(35 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
== Features of Zenity ==
 
== Features of Zenity ==
  
There are many different types of dialog boxes that the Zenity command can create. The following is a table that uses options (starting with -- symbol immediately followed by a word to indicate the type dialog box to create:
+
There are many different types of dialog boxes that the Zenity command can create. The following is a table that uses options (starting with -- symbol immediately followed by a word to indicate the type dialog box to generate. You are encouraged to use the online man pages to refer to all options, but here are a couple of options:<br /><br />
  
 
<table border="1" cellpadding="10">
 
<table border="1" cellpadding="10">
 
  <tr>
 
  <tr>
     <th>--calenda</th>
+
     <th>--calendar</th>
 
     <td>Display calendar dialog</td>
 
     <td>Display calendar dialog</td>
 
  </tr>
 
  </tr>
Line 20: Line 20:
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--error</th>
     <td></td>
+
     <td>Display error dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--file-selection</th>
     <td></td>
+
     <td>Display file selection dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--info</th>
     <td></td>
+
     <td>Display info dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--list</th>
     <td></td>
+
     <td>Display list dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--notification</th>
     <td></td>
+
     <td>Display notification icon</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--progress</th>
     <td></td>
+
     <td>Display progress indication dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--question</th>
     <td></td>
+
     <td>Display question dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--text-info</th>
     <td></td>
+
     <td>Display text information dialog</td>
 
  </tr>
 
  </tr>
 
       <tr>
 
       <tr>
     <th></th>
+
     <th>--warning</th>
     <td></td>
+
     <td>Display warning dialog</td>
 
  </tr>
 
  </tr>
 
  <tr>
 
  <tr>
     <th></th>
+
     <th>--scale</th>
     <td></td>
+
     <td>Display scale dialog</td>
 
  </tr>
 
  </tr>
 
</table>
 
</table>
 
             
 
  
      --error
 
              Display error dialog
 
  
      --file-selection
 
              Display file selection dialog
 
  
      --info Display info dialog
+
Adding the '''--text''' option provides the dialog box to contain text to provide information for the user (output, or helping guide the user when prompting for a question).
  
      --list Display list dialog
 
  
      --notification
 
              Display notification icon
 
  
      --progress
+
== Don't Make it Complicated ==
              Display progress indication dialog
 
  
      --question
+
Using Zenity is easy - don't make it complicated!
              Display question dialog
 
  
      --text-info
+
For example, think of Zenity as a replacement for the '''echo''' command to display output.
              Display text information dialog
+
You can use '''--info''' to display standard input, and '''--error''' to display standard error messages.
  
      --warning
+
To obtain input, think of Zenity as a replacement for the '''read''' command.
              Display warning dialog
+
You can use '''--entry''' to prompt the user for input. When the user enters text and clicks the OK button, or presses the <ENTER> key, zenity will return the value of that entered text.
  
      --scale
+
 
              Display scale dialog
+
All of the zenity dialog boxes that prompt users for information, return that information as standard output (stdout). '''Therefore, to store it into a <u>variable</u> to be used later in the program, you can use command substitution (enough said, since this is being used for an assignment! You figure it out :) ...  )'''.
 +
 
 +
 
 +
To obtain more options with this command, you should use the man pages... (eg. '''man zenity''').
 +
 
 +
== Simple Examples ==
 +
 
 +
<u>Displaying Output</u>
 +
 
 +
<table>
 +
  <tr>
 +
  <td>'''zenity --info --text "Here is an example of using zenity command to display text in a dialog box instead instead of using echo to display in a shell..."'''</td>
 +
  </tr>
 +
  <tr>
 +
  <td>Here is output from above command:<br /><br />[[Image:zenity_info.png|left|400px]]</td>
 +
  </tr>
 +
</table>
 +
 
 +
<u>Prompting User for Input</u>
 +
 
 +
<table>
 +
  <tr>
 +
    <td>'''zenity --entry --text "Please enter your full name"'''</td>
 +
  </tr>
 +
  <tr>
 +
    <td>Here is input dialog box for user to enter input:<br /><br />[[Image:zenity_entry.png|left|200px]]</td>
 +
  </tr>
 +
</table>
 +
 
 +
 
 +
<u>Prompting User (Menu Selection)</u>
 +
 
 +
<table>
 +
  <tr>
 +
    <td>'''zenity  --list  --radiolist  --text "&lt;b&gt;Please&lt;/b&gt; make a selection:" --hide-header  --column "Buy" --column "Item" FALSE "Door 1" FALSE "Door 2" FALSE "Door 3" FALSE Quit'''</td>
 +
  </tr>
 +
  <tr>
 +
    <td>Here is input dialog box for user to make a selection from a menu:<br /><br />[[Image:zenity_selection.png|left|300px]]</td>
 +
  </tr>
 +
</table>
 +
 
 +
 
 +
 
 +
 
 +
<u>Piping Command Output to a Graphical Display</u>
 +
 
 +
<table>
 +
  <tr>
 +
    <td>'''cal | zenity --text-info'''</td>
 +
  </tr>
 +
  <tr>
 +
    <td>Here is input dialog box to display stdout from a command graphically as opposed to in a shell:<br /><br />[[Image:zenityPipe.png|left|300px]]</td>
 +
  </tr>
 +
</table>
 +
 
 +
 
 +
 
 +
 
 +
<u>Displaying Text Over Several Lines</u>
 +
 
 +
<table>
 +
  <tr>
 +
    <td>'''zenity --info --text "Sentence1\n\nSentence2\n\nSentence3"'''</td>
 +
  </tr>
 +
  <tr>
 +
    <td>Here is output from above command. Notice how each sentence appears on a separate line. The "\n" character represents a new-line:<br /><br />[[Image:newlines.png|left|150px]]</td>
 +
  </tr>
 +
</table>
 +
 
 +
Have fun :)
 +
 
 +
Murray Saul

Latest revision as of 11:25, 17 July 2011

Definition of Zenity

Zenity is a Linux / Unix command that uses dialog boxes to make shell script more graphical to end-users.

Here is a link to the Wikipedia definition of the Zenity command: http://en.wikipedia.org/wiki/Zenity

Features of Zenity

There are many different types of dialog boxes that the Zenity command can create. The following is a table that uses options (starting with -- symbol immediately followed by a word to indicate the type dialog box to generate. You are encouraged to use the online man pages to refer to all options, but here are a couple of options:

--calendar Display calendar dialog
--entry Display text entry dialog
--error Display error dialog
--file-selection Display file selection dialog
--info Display info dialog
--list Display list dialog
--notification Display notification icon
--progress Display progress indication dialog
--question Display question dialog
--text-info Display text information dialog
--warning Display warning dialog
--scale Display scale dialog


Adding the --text option provides the dialog box to contain text to provide information for the user (output, or helping guide the user when prompting for a question).


Don't Make it Complicated

Using Zenity is easy - don't make it complicated!

For example, think of Zenity as a replacement for the echo command to display output. You can use --info to display standard input, and --error to display standard error messages.

To obtain input, think of Zenity as a replacement for the read command. You can use --entry to prompt the user for input. When the user enters text and clicks the OK button, or presses the <ENTER> key, zenity will return the value of that entered text.


All of the zenity dialog boxes that prompt users for information, return that information as standard output (stdout). Therefore, to store it into a variable to be used later in the program, you can use command substitution (enough said, since this is being used for an assignment! You figure it out :) ... ).


To obtain more options with this command, you should use the man pages... (eg. man zenity).

Simple Examples

Displaying Output

zenity --info --text "Here is an example of using zenity command to display text in a dialog box instead instead of using echo to display in a shell..."
Here is output from above command:

Zenity info.png

Prompting User for Input

zenity --entry --text "Please enter your full name"
Here is input dialog box for user to enter input:

Zenity entry.png


Prompting User (Menu Selection)

zenity --list --radiolist --text "<b>Please</b> make a selection:" --hide-header --column "Buy" --column "Item" FALSE "Door 1" FALSE "Door 2" FALSE "Door 3" FALSE Quit
Here is input dialog box for user to make a selection from a menu:

Zenity selection.png



Piping Command Output to a Graphical Display

cal | zenity --text-info
Here is input dialog box to display stdout from a command graphically as opposed to in a shell:

ZenityPipe.png



Displaying Text Over Several Lines

zenity --info --text "Sentence1\n\nSentence2\n\nSentence3"
Here is output from above command. Notice how each sentence appears on a separate line. The "\n" character represents a new-line:

Newlines.png

Have fun :)

Murray Saul