KDialog

This topic was published by and viewed 1825 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts

  • DevynCJohnson
    Keymaster
    • Topics - 437
    • @devyncjohnson

    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.

    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.

    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.

    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.

    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.

    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.

    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 -

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

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

    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 -

    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!

Viewing 1 post (of 1 total)