KDialog - KDE GUI for Scripts

Which script GUI do you prefer?

  • dialog

    Votes: 0 0.0%
  • xdialog

    Votes: 0 0.0%
  • kdialog

    Votes: 0 0.0%
  • zenity

    Votes: 0 0.0%
  • ncurses

    Votes: 0 0.0%
  • Something else

    Votes: 0 0.0%

  • Total voters
    0
D

DevynCJohnson

Guest
Some of you may have read my article about Zenity and thought to yourself that a GTK-based GUI for scripts is nothing compared to a Qt-based or KDE-like interface. Such people may not want to learn Zenity because they prefer a Qt-based interface. Okay, I can satisfy those people's needs. This article is for you KDE and Qt lovers.

The shell command for using KDE-looking dialogs is called "kdialog". After executing kdialog, the "$?" variable will equal zero (0) if successful and one (1) if cancel was pressed. The many types of errors produce different error codes. The "$?" variable allows scriptwriters to make scripts with decision constructs that handle errors and events differently from one another. For instance, a script can be made to perform a special action if the cancel-button was pressed.

Windows can be attached to others by adding this string of code after "kdialog" - ${WINDOWID:+--attach $WINDOWID}.

The kdialog command accepts different parameters that produce different windows as you will read about. One parameter that is accept by all windows is the "--title" parameter which makes the title; otherwise, the default is used.

Some message windows (like warnings, info, etc.) accept the argument "--dontagain myscript:nofilemsg" which allows the user to click the option to not show the window again. This works by storing a variable in a config file for your script. The "myscript" is the name you the script to which this argument applies.

To make a password-window, type a command like this - 'kdialog --title "TITLE" --password "YOUR INFO TEXT HERE"'. Scriptwriters obviously will want to save the password. The input can be saved to a variable or piped to a file.

Code:
kdialog --password "YOUR INFO TEXT HERE" > ./file

password=`kdialog --password "YOUR INFO TEXT HERE"`

Three different message boxes are available that have a single button (Okay) - information (--msgbox), warning/small (--sorry), and error (--error). The differences lie in the symbol on the window; either a blue info circle, yellow warning triangle, or a red error square, respectively.

Code:
kdialog --title "OPTIONAL TITLE" --msgbox "INFO"

kdialog --title "OPTIONAL TITLE" --sorry "WARNING"

kdialog --title "OPTIONAL TITLE" --error "ERROR"

Variations of these same windows exist. "--yesno" is like "--msgbox", but with a "yes" and "no" button. "--yesnocancel" is like "--yesno", but with the addition of a cancel button. "--warningyesnocancel" is like "yesnoconcel", but is a warning window rather than an info window. "--warningyesno" is like "--sorry", but with a "yes" and "no" button. "--warningcontinuecancel" has a "continue" and "cancel" button.

NOTE: For these windows, the value of "$?" is "1" for No, "2" for Cancel, and "0" for Okay, OK, Yes, and Continue.

kdialog also supports notification bubbles by using the "--passivepopup". This argument accepts a parameter in the form of a number that determines how many seconds the box remains visible until disappearing. This means a parameter of "7" will make the box disappear in seven seconds.

Code:
kdialog --title "OPTIONAL TITLE" --passivepopup "MESSAGE" 10

kdialog can also be used to display text files via the "--textbox" window. This argument also accepts dimensions for the window which are optional. kdialog --textbox /PATH/FILE.txt WIDTH HEIGHT

Files can be browsed and selected using "--getopenfilename". The script can specify the path to start in by default; otherwise, the current directory is the fall-back. The file browser can also be limited by file extension. This means if the script should only deal with mp3 files, then the programmer can filter by file extension ('*.mp3') or MIME type (audio/mp3). Multiple filters can be applied ('*.png *.jpg *.gif'). The "--getopenurl" argument is nearly the same as "--getopenfilename". Also, "--getexistingdirectory" is used to select a directory. "--getsavefilename" is also similar, but is used to save a file and make a new file.

Code:
kdialog --getopenfilename /PATH/TO/BE/IN/ '*.EXTENSION'

To make a script accept a typed input, use the "--inputbox" argument which makes an input window where the user can type some data.

Code:
username=`kdialog --title "ENTER YOUR NAME" --inputbox "Who are you?" "ENTER THE DEFAULT TEXT TO SHOW IN THE BOX HERE"`

Menu windows (--menu) require a little extra syntax. In the code seen below, if ITEM1 is clicked, the script will receive stdout1.

Code:
kdialog --menu "MESSAGE" stdout1 "ITEM1" stdout2 "ITEM2"

"--radiolist" is another type of menu window. Only one item can be selected.

"--checklist" is just like "--menu", but more than one item can be selected. Since the script may receive more than one set of data, the argument "--separate-output" can be used to set the character or string that separates each piece of data. For instance, "--separate-output +" will divide each data segment with a plus-sign like so - "stdout1+stdout2".

The "--combobox" has a drop-down menu with a list of items from which to select. The syntax looks like this -

Code:
kdialog --combobox "MESSAGE:" "ITEM0" "ITEM1" "ITEM2" "ITEM3" "ITEM4"

Progress windows are also possible with Kdialog. To create the window, use code like this

Code:
progress=`kdialog --progressbar "Starting" 10`

The "dbusref" variable will be used to show the changing progress. To make the progress bar change, use a series of commands like this -

Code:
qdbus $progress set "" value 1
qdbus $progress setLabelText "MESSAGE"
SOME-TIME-CONSUMING-TASK

The above code makes the progress bar move one unit out of 10 (as stated in the kdialog command since we typed 10). The next line produces the message associated with the current progress. Programmer could make the message state the current action being performed. The next line contains a command(s) that takes some time to complete. After that code, the script can have another qdbus command to increment the progress bar's number from 1 to the desired number (2 if you wish). Once the script is done with the progress bar, the window can be closed with this command - qdbus $progress close

Some special features can be added to your progress bar. To be able to cancel the ongoing task, add this line to your code before using the other qdbus commands - qdbus $progress showCancelButton true

If you do not like qdbus and prefer dcop instead, then use the same code, but make these replacements -

qdbus -> dcop
setLabelText -> setLabel
Set "" value 2 -> setProgress 2


Now, scriptwriters can use a KDE-like Qt interface with their script instead of the GTK-based interface called Zenity. Happy scripting!

DEBATE: In the survey near the top of the page, which script GUI do you think is the best and why?
 

Attachments

  • slide.jpg
    slide.jpg
    40 KB · Views: 113,731
Last edited:


One of our best members (Cyber-Berserker) has stopped coming to this site. I would like you all to give him a thanks for his great contributions to this site by posting a thank you or something of that manner to him on this thread.

Thanks @Cyber-Berserker for your great contributions (like this http://www.linux.org/threads/tips-for-the-fresh-born-linux-newbie.5613/) and many others. I am sure many of the answers you gave in the forums were greatly appreciated by the original-posters and those that read the threads.
 
One of our best members (Cyber-Berserker) has stopped coming to this site. I would like you all to give him a thanks for his great contributions to this site by posting a thank you or something of that manner to him on this thread.

Thanks @Cyber-Berserker for your great contributions (like this http://www.linux.org/threads/tips-for-the-fresh-born-linux-newbie.5613/) and many others. I am sure many of the answers you gave in the forums were greatly appreciated by the original-posters and those that read the threads.

Indeed I would like to thank Cyber-Berserker for his newbie tutorial. I have just started visiting this site and find it to be very good for me just now coming to use Linux in my daily life. If it were not for people like Cyber-Berserker it would make acclimating to Linux a great deal harder. Thanks to Devyn and the other staff that contribute to educating others about Linux and how to really learn to use it to accomplish their goals. :)
 

Staff online

Members online


Latest posts

Top