Programming - Python QT

Simple example

#!/usr/bin/env python
import sys, time
from qt import *

  
class MainWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        self.setCaption("Window title")
        self.layout = QGridLayout(self, 4, 2, 5, 10)
        
        # Create a multiline textbox
        self.textcontrol = QTextEdit(self)
        self.textcontrol.setTextFormat(Qt.PlainText)
        
        # Create a label and some buttons
        self.label1 = QLabel("", self)
        self.button1 = QPushButton("&Get data", self)
        self.quit = QPushButton("&Quit", self)
        
        # Add everything to the window
        self.layout.addWidget(self.textcontrol, 0, 1)
        self.layout.addWidget(self.label1, 1, 1)
        self.layout.addWidget(self.button1, 2, 1)
        self.layout.addWidget(self.quit, 3, 1)
        
        # Decide what to do when buttons are pressed
        self.connect(self.button1, SIGNAL("clicked()"),
                     self.do_something)
        self.connect(self.quit, SIGNAL("clicked()"),
                     self.close)
    
    def do_something(self):
        """Example of function which updates parts of the window,
        and is called when the button is pressed"""
        self.textcontrol.append(self.getdata())
        self.label1.setText(str(self.getdata()))
    
    def getdata(self):
      """Outline of data-source function"""
      return time.ctime()
    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.connect(app, SIGNAL('lastWindowClosed()'), app,
                  SLOT('quit()'))
    window = MainWindow()
    window.show()
    app.setMainWidget(window)
    app.exec_loop()

Screenshot

Screenshot of that program running on my computer

Notes

Basically create a load of QObjects, and add them to the screen. See the QT documentation site for a list of what's available and what functions they have

the "print" function works, and can send stuff to the console. Useful for debugging

QT seems to be the "prettiest" GUI library around. Better than Gnome/GTK+. Lots better than Tk. Available for Mac, Windows, Linux.

For more documentation, type "pydoc -p 1234", point your browser to "http://localhost:1234/", and look for the "QT" area