Development/Languages/Python/PyKDE Knotify Tutorial

< Development‎ | Languages‎ | Python
Revision as of 23:06, 9 August 2010 by Hefee (Talk | contribs)

Jump to: navigation, search



Python KNotify Tutorial
Tutorial Series   Python
Previous   None
What's Next  
Further Reading   Qt Signals and Slots in python, Introduction to PyQT4, Using PyKDE4


The aim of this tutorial is to give a sample class to include Notifications and Jobs to your own python programms, so the user can see what is going on (for example a backup system, that tells you that is backuping right now). It assumes a basic working knowledge of Python and PyKDE4. For now only the Job part is mentioned, 'cause that is the only part I've created right now.

Further plans

To add the Notification part also to this tutorial.


We start with a minimal KDE Appliciation (Using PyKDE4):

  1. ! /usr/bin/python
  2. -*- coding: utf-8 -*-

if __name__ == "__main__":

   import sys
   from PyKDE4.kdecore import KCmdLineArgs, KAboutData, KLocalizedString, ki18n
   from PyKDE4.kdeui import KApplication    
   appName     = "default"
   catalog     = ""
   programName = ki18n ("default")  
   version     = "1.0"
   description = ki18n ("Default Example")  
   license     = KAboutData.License_GPL
   copyright   = ki18n ("(c) 2010 Sandro Knauß") 
   text        = ki18n ("none") 
   homePage    = ""
   bugEmail    = ""
   aboutData   = KAboutData (appName, catalog, programName, version, description,
                             license, copyright, text, homePage, bugEmail)

   KCmdLineArgs.init (sys.argv, aboutData)
   app = KApplication ()

Job class

The first step is to have a look to the [1]. The intersting on is:

So it is easy to write a small Job-Class:

from PyQt4.QtCore import QObject,QTimer,QString from PyKDE4.kdecore import KJob

class MyJob(KJob):

   def __init__(self,parent=QObject()):
       #We want to have a Suspenable and Killable Job
       #Just a maximum Variable
   def start(self):
       #initalizing for work
       #start the actual work in another thread - this function has to terminate, before the work is done
       QTimer().singleShot( 0, self.doWork )
   def doWork(self):             
     #the actual work                                         
           #if we are killed or suspended just return                                           
           if self.error() or self.isSuspended():                    
       except RuntimeError:
           #if this class is killed before this functions is called a RuntimeError will raise
       #do a peace of hard work
       #fortunately we have made a peace of work -> show this to everybody
       if self.i==self.max:
           #jeah we have done the bunch of work
           #send the result signal for showing, that we ended the work
       #just go to sleep for 1000ms than go on
       QTimer().singleShot( 1000, self.doWork )
   def doSuspend(self):
       #the surounding function from KJob makes the isSuspended() become True
       #returns True for signaling that this class supports to suspend
       return True
   def doResume(self):
       #start with work again
       QTimer().singleShot( 0, self.doWork )
       #return True for signaling that this class supports resuming
       return True
   def doKill(self):
       #return True for signaling that we support killing
       return True

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal