Inside Python’s Qt App

Today I found the sketch of Mr Creosote on youtube, because of that, I made a choice that this would the only reference to this sketch. So for today’s post let’s create a quick Qt tool to open a website inside. You can use this as a quick shortcut to the best blog currently on the web (this of course :) ), or to use it as a quick shortcut to less known websites like Youtube or Facebook.

Here is a screen shot of the app we will develop here:

And here is the code:

#!/usr/bin/env python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class InsidePythonApp:
    def Main(self):

        #Initializes the window system and constructs an application object
        #with command line parameter.
        app = QApplication(sys.argv)

        #Constructs an empty QWebView widget without parent
        self.web = QWebView()

        #Gets the main window of our app
        self.window = QMainWindow()

        #Lets create our menu
        menu = QMenu("Navigate", self.window)

        homeAction = QAction('Home', menu, checkable=False)
        homeAction.triggered.connect(self.home)
        menu.addAction(homeAction)

        backAction = QAction('Back', menu, checkable=False)
        backAction.triggered.connect(self.back)
        menu.addAction(backAction)

        forwardAction = QAction('Forward', menu, checkable=False)
        forwardAction.triggered.connect(self.forward)
        menu.addAction(forwardAction)

        exitAction = QAction('Exit', menu, checkable=False)
        exitAction.triggered.connect(self.exit)
        menu.addAction(exitAction)

        self.window.menuBar().addMenu(menu)

        #Sets the title of the window
        self.window.setWindowTitle("Inside Python's Widget")

        self.window.setCentralWidget(self.web)

        self.window.show()

        #Opens the best blog in the world in the window
        self.web.load(QUrl("http://insidepython.wordpress.com"))

        sys.exit(app.exec_())

    def home(self):
      self.web.load(QUrl("http://insidepython.wordpress.com"))

    def back(self):
      self.web.Back()

    def forward(self):
      self.web.Forward()

    def exit(self):
      self.window.close()

if __name__ == "__main__":
    app = InsidePythonApp()
    app.Main()

So in this post we covered:

  • Creation of a window
  • Adding a widget to a window
  • Adding a menu to the window
  • Connecting actions to menus

About the WebKit, you can find some more information here. And you can visit Sourceforge to download this and previous sources. Hope you guys liked this post, it can be a good starting point to start your applications.

Please leave your comments

Cheers


jobhunthero

Job Search, Resume and Interview advice from the trenches...

Cocheno

Another Python Tutorial

Engineer's Journey

From Bachelor to Master

Inside Python

Another Python Tutorial

Rui Jarimba

Yet another geek blog about software development

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: