Development/Tutorials/SuperKaramba

    From KDE TechBase
    Revision as of 07:47, 23 April 2007 by Dipesh (talk | contribs) (sample++)

    SuperKaramba

    Intro

    SuperKaramba is a tool that allows one to easily create functionality enhancement modules on a KDE desktop. Such modules are interactive programs written in Python or Ruby that are usually embedded directly into the background and do not disturb the normal view of the desktop.

    Links

    Examples

    Clock with Ruby and Python

    Let's take a look at one of them, the clock.rb theme written in Ruby. The theme just displays the current time in a RichText widget.

    require 'karamba'
    
    def initWidget(widget)
        Karamba.resizeWidget(widget, 300, 120)
        @richtext = Karamba.createRichText(widget, Time.now.to_s)
        Karamba.moveRichText(widget, @richtext, 10, 10)
        Karamba.setRichTextWidth(widget, @richtext, 280)
        Karamba.redrawWidget(widget)
    end
    
    def widgetUpdated(widget)
        puts  >> widgetUpdated"
        Karamba.changeRichText(widget, @richtext, Time.now.to_s)
        Karamba.redrawWidget(widget)
    end
    

    The initWidget method will be called once if the widget got initialized. Here we setup the RichText widget where we display the current time in. The widgetUpdated will be called each second once (the interval is defined in the clock.theme themefile) and just updates the text display in the RichText widget with the new time.

    Let's take a look at another theme. The text.py theme written in Python just displays some text widgets. We take this is example to create our own script, that does the same as the clock.rb above, that is to display the current time within a text widget.

    import karamba, time
    
    text = None
    
    def initWidget(widget):
        text = karamba.createText(widget, 0, 20, 200, 20, "Text meter")
    
    def widgetUpdated(widget):
        t = time.strftime("%Y-%M-%d %H:%M.%S")
        karamba.changeText(widget, text, t)
    

    In the initWidget method we create our text widget that is updated once per second (or per interval as defined in the matching theme file) at the widgetUpdated method to display the new current time.

    Python with TkInter

    While you are also able to use <a href="">PyQt</a> which doesn't look only nicer but also provides the nice Qt-API pythonized, you are also able to use the default toolkit Python comes with, that is TkInter.

    The following sample just displays a TkInter "hello world" dialog if you click on the widget.

    import karamba
    
    def widgetClicked(widget, x, y, button):
        from Tkinter import *
        root = Tk()
        w = Label(root, text="Hello, world!")
        w.pack()
        root.mainloop()
    

    QtRuby/Korundum with Ruby

    The following sample script written in Ruby demonstrates that you are able to use QtRuby/Korundum within your Ruby scripts.

    require 'karamba'
    require 'Qt'
    
    class Dialog < Qt::Dialog
        def initialize
            super()
            self.windowTitle = 'Hello World'
        end
    end
    
    def widgetClicked(widget, x, y, button)
        dialog = Dialog.new
        dialog.exec
    end
    

    The script does implement only the widgetClicked function that got called if the user clicks on the widget. What we do within that function is to create an instance of the Dialog class that implements a QDialog using QtRuby and then execute that modal dialog.