PyQT – Link UI to Py

When using QtDesigner (http://doc.qt.nokia.com/4.7/designer-manual.html) you need to link the GUI that you have created to the rest of your Python code. The script below provides the absolute minimum that is needed to do this linking and show the GUI on the screen.

One needs to have created a GUI, saved it as a .ui file and then run pyuic4 on the .ui file. The mkpyqt.py/Make PyQt tools can be used to do this if you have them. The result will be a ui_filename.py file than is then imported into your main application (see below). The GUI class in the application needs to inherit the QWidget, QDialog or QMainWindow class as well as the UI class.

from PyQt4.QtCore import *

from PyQt4.QtGui import *

import ui_FindPy # where ui_FindPy is the output from Make PyQt/pyuic4

class FindPy(QDialog, ui_FindPy.Ui_Dialog): # start new class. Inherit from QDialog and form in UI file
def __init__(self, parent=None): # set __init__
super(FindPy, self).__init__(parent) # set super where FindPy is class name
self.setupUi(self) # set form setup method
if __name__ == "__main__": # initialise the GUI, show it and start the event loop
import sys

app = QApplication(sys.argv)

form = FindPy()
form.show()
app.exec_()

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: