Languages/Python/PyKDE WebKit Tutorial/Part3: Difference between revisions

From KDE TechBase
(New page: So far our application has a very flat structure. This will not scale well, if we kept this flat structure as our programme grew we would soon get lost in our code. So we will create a c...)
 
(Mark for archiving)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Archived}}
{{Warning|This tutorial uses Qt4, PyQt4, and PyKDE4.}}
So far our application has a very flat structure.  This will not scale well, if we kept this flat structure as our programme grew we would soon get lost in our code.  So we will create a class so hold our web browser.
So far our application has a very flat structure.  This will not scale well, if we kept this flat structure as our programme grew we would soon get lost in our code.  So we will create a class so hold our web browser.


Line 5: Line 8:
Add this after the import lines:
Add this after the import lines:


<code python>
<syntaxhighlight lang="python">
class Tutorial:
class Tutorial(object):
     def __init__(self):
     def __init__(self):
         self.web = QWebView()
         self.web = QWebView()
         self.web.load(QUrl("http://kubuntu.org"))
         self.web.load(QUrl("http://kubuntu.org"))
         self.web.show()
         self.web.show()
</code>
</syntaxhighlight>


This is a class called Tutorial which contains a constructor method (a subroutine which is run when an object is created using this class) which in Python is always called __init__.  The constructor creates our ''QWebView'' widget as before.  The widget is assigned to ''self.web'', the ''self'' means that variable belongs to the class, without it the variable would be deleted at the end of the constructor.  All class methods in Python take ''self'' as their first argument.
This is a class called Tutorial which contains a constructor method (a subroutine which is run when an object is created using this class) which in Python is always called __init__.  The constructor creates our ''QWebView'' widget as before.  The widget is assigned to ''self.web'', the ''self'' means that variable belongs to the class, without it the variable would be deleted at the end of the constructor.  All class methods in Python take ''self'' as their first argument.
Line 19: Line 22:
Remove the QWebView lines after creating the KApplication add this:
Remove the QWebView lines after creating the KApplication add this:


<code python>
<syntaxhighlight lang="python">
app = KApplication()
app = KApplication()
tutorial = Tutorial()
tutorial = Tutorial()
</code>
</syntaxhighlight>


which creates an object out of our Tutorial class.
which creates an object out of our Tutorial class.
Line 32: Line 35:
[[image:Pykde-tutorial-2.png|center]]
[[image:Pykde-tutorial-2.png|center]]


[[Development/Languages/Python/PyKDE_WebKit_Tutorial/Part2|« Back to Part 2]] |
[[Development/Languages/Python/PyKDE_WebKit_Tutorial/Part4|On to Part 4 »]]
[[Development/Languages/Python/PyKDE_WebKit_Tutorial/Part4|On to Part 4 »]]
[[Category:Python]]

Latest revision as of 14:03, 31 May 2019


This page has been archived
The information on this page is outdated or no longer in use but is kept for historical purposes. Please see the Category:Archives for similar pages.
Warning
This tutorial uses Qt4, PyQt4, and PyKDE4.


So far our application has a very flat structure. This will not scale well, if we kept this flat structure as our programme grew we would soon get lost in our code. So we will create a class so hold our web browser.

If you are unfamiliar with object orientated programming, a class is a collection of methods (subroutines) and variables. When you create a variable using that class it is called an object.

Add this after the import lines:

class Tutorial(object):
    def __init__(self):
        self.web = QWebView()
        self.web.load(QUrl("http://kubuntu.org"))
        self.web.show()

This is a class called Tutorial which contains a constructor method (a subroutine which is run when an object is created using this class) which in Python is always called __init__. The constructor creates our QWebView widget as before. The widget is assigned to self.web, the self means that variable belongs to the class, without it the variable would be deleted at the end of the constructor. All class methods in Python take self as their first argument.

Python is very concious about line spacing, it does not use curly brackets like C++ or Java but uses line indents to mark blocks of code. So you must keep your indents consistent, I use four spaces for indents. You may prefer tabs but always use the same throughout your code.

Remove the QWebView lines after creating the KApplication add this:

app = KApplication()
tutorial = Tutorial()

which creates an object out of our Tutorial class.

See the full code.

This version will look the same as the previous.

« Back to Part 2 | On to Part 4 »