Wednesday, October 6, 2010
4:24 PM

Easiest GUI programming with KDIALOG

If you are using a somewhat modern version of KDE, chances are the impressive kdialog application is already part of your system. This apparently harmless program has got some quite impressive moves and can provide the 'ilusion' of visual programming. In fact, if you thought bash scripting was limited to that boring terminal, you are in for a ride.

KDIALOG

kdialog is an application that provides a framework of messages and prompts that can be triggered from the command line. It is similar to zenity under GNOME, but more powerful and flexible.

As a quick example, let's create your typical "hello world!" message using kdialog. Open a virtual terminal window and type the following command (Using single quotes here is important to avoid any kind of expansion).

kdialog --msgbox 'Hello World!'


Click on image to enlarge.

Now, that doesn't look very useful, does it? A command that can trigger a GUI message? What for? Well, the good news is that we can use the many interactive options kdialog provides, get input from the user and build our script around it. By doing so, we would be able to write simple GUI "applications".

LET'S BUILD A SIMPLE MENU...

For the purpose of this article, I created a very short script that builds a simple menu and triggers certain specific actions based on the user's input. Note that this script is only an example, designed to show a bit of what kdialog is capable of.

The idea is to present the user with a simple welcome popup. If the user chooses to continue, s/he will be offered a few options to choose from. Each option will then trigger an action. Should the user choose to exit, a goodbye message will show up.


Click on image to enlarge.

The menu above is clearly too simplistic but remember this is only an example. Just be creative and find options that make sense for you.

WHAT ABOUT THE SCRIPT?

I have entered the code from this simple script below, so you can review it and be sure it's harmless.
#!/bin/bash
# Simple script to play with kdialog options

function AppMenu () {
# Display menu and interact based on the user's input

var2="$(kdialog --menu "Please, choose from one of the options below:" 1 'Open a File' 2 'Listen to Music' 3 'Watch Videos')"

if [[ $? -eq 0 ]]; then
# If the user did not cancel, find out what s/he chosed
case $var2 in
1)
# open an open file dialog
kdialog --getopenfilename /home/shred;;
2)
# open Amarok
amarok &;;
3)
# Open the video file the user picked up with VNC
var3="$(kdialog --getopenfilename /home/shred/Videos)"
if [[ $var3 ]]; then
vlc "$var3" &
fi
;;
esac
else
# Show the text message in file 'text_message.txt'
# Note that text_message.txt should be under the same location as the script!
kdialog --textbox text_message.txt
fi

return; }

# Main Program
# Welcome the user, allow for early exit if not interested
kdialog --title "Welcome to Switchbox" --yesno "Welcome to Switchbox, the script that will let you choose what to do. \nPlease, click OK to Continue or CANCEL to exit"

# If the user decided to continue, present a menu
if [[ $? -ne 1 ]]; then
AppMenu
fi

If you want to see how this script works (remember you should be using KDE or at the very least have KDialog installed) simply copy the script text below and paste it into a new text file (which you may create using kwrite, gedit or whatever your favorite editor is). Save that file as KDialog.sh. Right click on the file you just saved and go to Properties > Permissions tab. Click on Advanced Permissions button and tick all three options for user. Once done, simply double click on the script icon to run it.

CREATIVITY AND FUN

Surely this script is far from being an example of optimum coding, but my aim was mainly to show that with a bit of creativity, kdialog can help push our scripts a long way. The idea is that scripts don't necessarily have to be restricted to the command line. As long as the functionality and interactivity is not extremely complex, we should be able to handle most of it using kdialog. Ultimately, this would allow us to have users with absolutely no understanding or interest in technology running a script and not being intimidated by it, perhaps not even knowing they are indeed running a script.

Once again, find the right use for you and have fun!

Thanks for reading.

0 comments:

Post a Comment