|
PythonProject
PyQt Project tutorial & tips
Users-Documentation IntroductionPython is a very popular interpreting programming language, it's developed to be an easy language with the features of the high level programming languages. PyQt notesPyQt 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 WorldThis part is a quick tutorial telling you how the basic things in PyQt are going. NotesThis 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 MonkeyStudioThe support of Python in MonkeyStudio is done through the Python Plugin, thus, you need to verfify that the plugin is enabled at first. Create The First ProjectNow, 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 ProjectOnce 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. 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. 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 = NoneThis is the Class del function. It removes the window after closing it. CODING!!!Before you start check Signals & Slots page to know more about signals & slots. 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. (1): MonkeyStudio | |

the last button on the toolbar
@pyqtSlot() should be without "", or it will throw a C++ error.
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!!!!
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!
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):
IndentationError?: unexpected indentIt 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.
Seems like python likes pure spaces as indents...
The next mks version ( 1.9.0.0 ) will have spaced indentation as default and no longer use tabs indentations as default. Thank you!
hi, Is this python 3.2 supported ?