Sunday, January 22, 2012

Qt Tutorial #1 with PyQt4 and Python 3

This is the first of a series of posts based on the TrollTech Qt Tutorial #1.  The posts will contain code from the original tutorial re-written in Python 3 using PyQt4.

It's an old tutorial, I suspect there are now better ways to handle the graphics (with QGraphicScene, QGraphicsView, QGraphicsItem, etc) but useful for getting a handle on layouts, signals, slots, and customizing event handlers.

Here's the first, Hello World.



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
    PyQt4 conversion of Qt Tutorial 1
    
last modified: 2012-01-19 jg
ref: http://doc.trolltech.com/3.3/tutorial1-01.html
'''

import sys
from PyQt4.QtGui import (QApplication, QPushButton)

def main():
    app = QApplication(sys.argv)
    hello = QPushButton("Hello")
    hello.resize(100, 30)

    #app.setMainWidget(hello)    # n/a in  PyQt
    hello.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()