Finally we start the application with ''app.exec_()''. In C++ this method is called just ''exec()'' but we need to add an underscore in Python because ''exec'' is a reserved word. ''exec_()'' will finish when you close the window, at which point it will return 0 (or another exit code if there are problems). This gets passed to Python's ''sys.exit()'' function which will quit the programme.

Finally we start the application with ''app.exec_()''. In C++ this method is called just ''exec()'' but we need to add an underscore in Python because ''exec'' is a reserved word. ''exec_()'' will finish when you close the window, at which point it will return 0 (or another exit code if there are problems). This gets passed to Python's ''sys.exit()'' function which will quit the programme.

−

[[Development/Languages/Python/PyKDE_WebKit_Tutorial/Part2|Part 2]]

+

It will look like this

+

[[image:Pykde-tutorial-1.png|frame|center]]

+

+

[[Development/Languages/Python/PyKDE_WebKit_Tutorial/Part2|On to Part 2 »]]

Revision as of 23:29, 12 October 2008

Creating a simple web browser application in PyKDE using the WebKit widget from Qt.

Python is a powerful yet intuitive programming language. PyQt and PyKDE are bindings which let you write GUI programmes using Qt and KDE widgets in Python.

Write this out in a file and save it as webkit1.py. Python applications are not complied, you can run this from a command line with python webkit1.py (or you can set it as executable and just run ./webkit1.py.

The first line of the code tells Linux to run the file with Python.

The next block imports some Python program libraries. sys contains useful functions to do with the system. The PyQt libraries contain GUI widgets and related code.

All Qt applications need to create a QApplication object. Our code here creates the object passing it sys.argv which contains and command line options used to start the application (none in our case) and saves it to a variable called app.

Next we create a QWebView object. QWebView is the widget from Qt which gives us a WebKit based browser. The QWebView documentation shows us methods which we can call on our object, the one we are interested in is load(QUrl) which loads a web page. So we call that in the next line. Then we use show() to display the widget, it has no parent widget so it will be displayed as a window on your desktop.

Finally we start the application with app.exec_(). In C++ this method is called just exec() but we need to add an underscore in Python because exec is a reserved word. exec_() will finish when you close the window, at which point it will return 0 (or another exit code if there are problems). This gets passed to Python's sys.exit() function which will quit the programme.