Pages

Saturday, February 16, 2013

PyQt 'Great Day' example


I'm a beginner at pyqt but I thought this posting may be helpful as a kind of 'hello world' example.
#!/usr/bin/env python

import sys
from PyQt4 import Qt

app = Qt.QApplication(sys.argv)


def foo():
    print "Great , Day"

window = Qt.QWidget()

#---------------------------
#set background color in rgb, add some buttons with a callback then show
#---------------------------
bgcol = Qt.QColor(105,105,105) 
window.setStyleSheet("QWidget { background-color: %s }" %bgcol.name()) 
window.setWindowTitle("Block Rig")
window.setGeometry(300,300,280,170)
layout = Qt.QVBoxLayout(window)
btn1 = Qt.QPushButton("Arm Rig",None)
btn2 = Qt.QPushButton("Leg Rig",None)
app.connect(btn1, Qt.SIGNAL("clicked()"), foo)
app.connect(btn2, Qt.SIGNAL("clicked()"), foo)
lab = Qt.QLabel("Great , Day")
cbox = Qt.QCheckBox("G&reat Day")
combox = Qt.QComboBox()
combox.addItem("A")
combox.addItem("B")
sbox = Qt.QSpinBox()

layout.addWidget(lab)
layout.addWidget(btn1)
layout.addWidget(btn2)
layout.addWidget(cbox)
layout.addWidget(combox)
layout.addWidget(sbox)

window.show()
app.exec_()


These are some tutorials that helped me write this:

Python Programming/PyQt4 (wikibook dot org)
Jan Bodnar (zetcode dot com)
David Boddie (boddie dot org dot uk)


These are what I downloaded for my personal macbook installed in this order: (python then qt libaries then last pyqt)
(Note after installing python 2.7.3 open ~/.bash_profile file and make sure that the python export path for this python is before all others. To tell if 2.7.3 is being used in a terminal typing python can read at top and see if it is)

python--
Mac OS X 64-bit/32-bit x86-64/i386 Installer (2.7.3) for Mac OS X 10.6 and 10.7 [2] (sig).
(http://www.python.org/download/releases/2.7.3/)

qt libraries --
4.8.2 Mac [cocoa](dmg – 182 MB) [releases.qt-project.org]
(http://qt-project.org/wiki/QtVersions)

pyqt --
PyQtX 4.9.4 Complete (Qt 4.8.2 included) for Python 2.7.3 (51.7 MB)
(http://sourceforge.net/projects/pyqtx/files/Complete/)




Hope this is helpful.

cheers,
Nate