My favorites | Sign in
Project Home Downloads Wiki
Search
for
  1. Introduction
  2. IDE Description
  3. Projects
  4. Debugging with MkS
  5. Editing your source code
    • Autocompletion
    • Abbreviations
    • Other useful functionality overview
  6. External tools
  7. Custom templates
  8. Qt developer tools
    • Embedded Qt Designer
    • Embedded Qt Assistant
    • QMake project (.pro) management
  9. Basic Plugins
  10. Child Plugins
    • Qt Assistant
    • Qt Designer
  11. Command Line Tool Plugins
  12. Builder Plugins
    • GNU Make
  13. Debugger Plugins
    • Beaver
    • MkS GDB Integration
  14. Interpreter Plugins
    • PHP
    • Python
  15. XUP Manager Plugins
    • [PHP-Qt PHP-Qt]
    • PyQt
    • QMake
    • XUP
  16. Other useful functionality
PythonProject  
PyQt Project tutorial & tips
Users-Documentation
Updated Sep 13, 2011 by pasnox

Introduction

Python is a very popular interpreting programming language, it's developed to be an easy language with the features of the high level programming languages.

As the python is getting more popular, a lot of the libraries that are designed originally for C & C++ had been ported to Python through binding libraries. One of these libraries is the PyQt which is the Python bindings for Qt. &, as MkS(1) supports Python language, it supports PyQt.

PyQt notes

PyQt is a multi-licensed Python library developed by RiverBank Computing Limited. For any use except for the commercial use, it's licensed under GPLv2. For the commercial use RiverBank computing provides a special-purpose designed commercial license. For more information: http://www.riverbankcomputing.co.uk

PyQt Hello World

This part is a quick tutorial telling you how the basic things in PyQt are going.

Notes

This tutorial expects that you have the basic knowledge in Python at least. If you don't know how the Python code looks like don't continue.

Setup MonkeyStudio

The support of Python in MonkeyStudio is done through the Python Plugin, thus, you need to verfify that the plugin is enabled at first.

Goto: Plugins > Interpreter > Python, & check Enabled.

Create The First Project

Now, you can create & open Python projects. Goto: Project > New, a Template Wizard will be shown. From the language menu choose Python. The left list will show you the Python projects templates MonkeyStudio provide, click on PyQt GUI. The right column of the dialog will show the set of settings needed to create the project. Change the project name to be: MonkeyStudio PyQt tutorial. Change the other options to fit your needs & click create.

Discovering the Project

Once you click create, the MkS will show you your created project. The projects Dockbar will show the project & its elements classified into two types: Qt Forms & Python Files. Qt Forms are the GUI forms you want to code, while, the Python Files are the codes. Expand Qt Forms & double click on mainwindow.ui, the Qt Forms designer will be shown. Activate the Widget Box dockbar to show the Qt GUI objects list & activate the Property Editor to show the objects properties bar. Change the windowTitle properety to: Hello World! to change the window's title.

Now, put some objects to get some fun. Do something like this:



If you want to see how your form is displayed to the user, simply, click on 'Preview in..' button on the designer toolbar, & choose Preview in Oxygen or any other style.

the last button on the toolbar

Now, we had the GUI ready for the coding. Return to the Project dockbar & double click on main.py. This will show the contents of the file. Lines description are as following:

import sys

# import PyQt4 QtCore and QtGui modules
from PyQt4.QtCore import *
from PyQt4.QtGui import *

This part of the code imports the needed libraries to be able to start the GUI.

from mainwindow import MainWindow

This part imports the GUI windows & forms. In this case we are importing the Windows Named MainWindow written in file mainwindow[.ui].

if __name__ == '__main__':

This conditional syntax verifies that the program is a standalone & had not been ran as a subprocess.

    # create application
    app = QApplication(sys.argv)
    app.setApplicationName('!MonkeyStudio !PyQt tutorial')

This part prepares the Qt to show the windows.

    # create widget
    w = MainWindow()
    w.setWindowTitle('!MonkeyStudio !PyQt tutorial')
    w.show()

This part prepares the window we imported before, & shows it.

    # connection
    QObject.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))

This part connects between the lastWindowClosed() signal & the quit() slot.

    # execute application
    sys.exit(app.exec_())

This part ends the program after executing the Qt application.

Mostly, the main.py file is a standard file, we don't have a lot of things to do with it, because, the most hard work is in the mainwindow.py, which is the coding part of the form we created before. Now, open the mainwindow.py. Lines description are as following:

from PyQt4 import uic

(Ui_MainWindow, QMainWindow) = uic.loadUiType('mainwindow.ui')

This imports the uic module from the PyQt library. This module is responsible from converting the UI file to something the PyQt can interact with.

class MainWindow (QMainWindow):
    """MainWindow inherits QMainWindow"""

This is the class block. Each form (or window) code part is a class which has functions.

    def __init__ (self, parent = None):
        QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

This is the Class init function. It prepares the window.

    def __del__ (self):
        self.ui = None

This is the Class del function. It removes the window after closing it.

This class is what exactly we will work on, because, in this class we will write how the window's Signals are being interpreted.

CODING!!!

Before you start check Signals & Slots page to know more about signals & slots.

Let's start with an easy thing. When we click on Continue button we want the console to print something. To do so, we need at first to import a module from the PyQt4. Add this line to the head of the file:

from PyQt4.QtCore import *

Now, we need to connect the click() signal with a slot, by, adding to the end of the init function this line:

        self.connect(self.ui.pushButton, SIGNAL('clicked()'), SLOT('click()'))

This function connects the clicked() signal of the pushButton (the name of the button) with the click() slot. The click() slot in is not yet there, so, put this piece of code at the last of the class:

    @pyqtSlot("")
    def click(self):
        print "welcome, " + self.ui.lineEdit.text() + "!!"

The first line is a decoration telling the PyQt, that, this is a slot. The second line is the function defining line. The third line is the function body, in it, we put the print command to print greetings for the name written inside the lineEdit object.

Now, all you need to do is playing with the signals & slots to get started.


(1): MonkeyStudio

Comment by vilian.a...@gmail.com, May 28, 2011

@pyqtSlot() should be without "", or it will throw a C++ error.

Comment by yankwiz...@gmail.com, Jul 12, 2011

thank you so much for this,I spent a week trying to get a good IDE for python and Qt and now here it is!!!!

Comment by yankwiz...@gmail.com, Jul 17, 2011

Hey,I am using monkey studio and python to do my final year project and am having problems connecting forms i created with the menu,can anyone help me?please if you can send me the codes on this e-mail it would be great!

Comment by zhhtc...@gmail.com, Aug 12, 2011

I try your step to make mainwindow.py like this

from PyQt4 import uic
from PyQt4.QtCore import *


(Ui_MainWindow, QMainWindow) = uic.loadUiType('mainwindow.ui')

class MainWindow (QMainWindow):
    """MainWindow inherits QMainWindow"""

    def __init__ (self, parent = None):
        QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
		self.connect(self.ui.pushButton, SIGNAL('clicked()'), SLOT('click()'))
		#self.connect(self.ui.pushButton, SIGNAL('clicked()'), self.click())
		
    def __del__ (self):
        self.ui = None

    @pyqtSlot()
    def click(self):
        print "welcome, " + self.ui.lineEdit.text() + "!!"

But it return the following error

Traceback (most recent call last):

File "main.py", line 7, in <module>
from mainwindow import MainWindow?
File "/home/zhukov/TEMP/New/mainwindow.py", line 14
self.connect(self.ui.pushButton, SIGNAL('clicked()'), SLOT('click()')) ^
IndentationError?: unexpected indent

Comment by zhhtc...@gmail.com, Aug 12, 2011

It must be a indent bug in MonkeyStdio?, It show the same indent as the above line in line 14. I used GEANY open mainwindow.py and also found the same indent, but I rewrited it later and the code worked.

Comment by theTr...@gmail.com, Sep 8, 2011

Seems like python likes pure spaces as indents...

Comment by project member pasnox, Sep 13, 2011

The next mks version ( 1.9.0.0 ) will have spaced indentation as default and no longer use tabs indentations as default. Thank you!

Comment by areos...@gmail.com, May 4, 2012

hi, Is this python 3.2 supported ?


Sign in to add a comment
Powered by Google Project Hosting