from PyKDE4 import kdecore
from PyKDE4 import kdeui
import sys
def createAboutData():
"""
Create a KAboutData with information about this application.
"""
return kdecore.KAboutData(
# Program name used internally
"hello",
# Catalog name
"",
# Displayable program name
kdecore.ki18n("Hello World app"),
# Program version
"0.1.0",
# Short description about the program
kdecore.ki18n("A simple KDE Hello World app"),
# Program license
kdecore.KAboutData.License_BSD,
# Copyright statement
kdecore.ki18n ("(c) 2009 Mario Boikov"),
# Free form text
kdecore.ki18n("Free form text\nsupporting newlines"),
# Home page address for this program
"http://www.pysnippet.com",
# Bug report email address
"mario@beblue.org",
)
def main():
about = createAboutData()
kdecore.KCmdLineArgs.init(sys.argv, about)
app = kdeui.KApplication()
# INSERT APP CODE HERE
# Start event loop
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Nothing special will happen if you execute this application, we need some kind of window/widget to display something. To accomplish this we can create a KMainWindow with a KPushButton. We'll also add code to print Hello World when the button is clicked. The following snippet will create a window with a push button:
from PyKDE4 import kdeui
from PyKDE4 import kdecore
from PyQt4 import QtCore
class MainWindow(kdeui.KMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# Create widget with vertical box layout manager
layout = kdeui.KVBox()
# Create push button and add to layout
button = kdeui.KPushButton(kdecore.i18n("Push Me!"), layout)
# Connect to button's 'clicked' signal
QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"),
self.buttonClicked)
self.setCentralWidget(layout)
def buttonClicked(self):
print "Hello World!"
You might wonder why I use ki18n() when creating the KAboutData and i18n() for the MainWindow. Both functions do translations, i18n() requires an instance of KApplication before use but ki18n() don't.To actually show the window we have to instantiate it and make it visible, the "INSERT APP CODE HERE" comment should be replaced with the following lines:
mainWindow = MainWindow()
mainWindow.show()
As you can see, it doesn't take too much effort to create a simple app in KDE. There's almost more code to create the KAboutData than doing the window with a push button.
Here's a screen shot of what the window looks like on my Kubuntu installation:If we compare Qt and KDE there aren't too much differences, at least with this example. The code below implements the same Hello World app made with Qt:
import sys
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, title):
super(MainWindow, self).__init__()
self.setWindowTitle(title)
centralWidget = QtGui.QWidget()
button = QtGui.QPushButton("Push Me!")
layout = QtGui.QVBoxLayout()
layout.addWidget(button)
centralWidget.setLayout(layout)
QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"),
self.buttonClicked)
self.setCentralWidget(centralWidget)
def buttonClicked(self):
print "Hello World!"
def main():
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow("Hello World App")
mainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
The biggest difference is how the layout is handled and the absence of About Data.
No comments:
Post a Comment