Simple PyQt demo from book doesn't work
Question: I'm brand new to GUI programming under Python and just got the book "Rapid GUI
Programming with Python and QT" by Summerfield. The very first simple example
("pop-up alert in 25 lines") on page 112 works, but my attempt to exactly
replicate the second example ("an expression evaluator in 30 lines") on page
116 produces only a blank window, with no visible fields for either entry or
output and not even a window title. This is under Mac OS X 10.8.5 using the
latest Enthought Canopy 64-bit Python installation (1.2.0.1610)

The complete contents of PyQtdemo.pyw is

    
    
    from __future__ import division
    import sys
    from math import *
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    class Form(QDialog):
        def _init__(self, parent=None):
            super(Form,self).__init__(parent)
            self.browser = QTextBrowser()
            self.lineedit = QLineEdit("Type an expression and press Enter")
            self.lineedit.selectAll()
            layout = QVBoxLayout()
            layout.addWidget(self.browser)
            layout.addWidget(self.lineedit)
            self.setLayout(layout)
            self.lineedit.setFocus()
            self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)
            self.setWindowTitle("Calculate")
    
        def updateUi(self):
            try:
                text = unicode(self.lineedit.text())
                self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
            except:
                self.browser.append("<font color=red>%s is invalid!</font>" % text)
    
    app = QApplication(sys.argv)
    print dir(app)
    form = Form()
    form.show()
    app.exec_()
    

It seems to me there are only the following possibilities:

  1. there's a typo in my code that I've overlooked;
  2. there's something wrong with how I'm invoking the script (e.g., "python PyQtdemo.pyw");
  3. there's something wrong with my PyQt 4.10.3-1 installation;
  4. there's an error in the book.


Answer: It's a typo in your code.

The `Form.__init__` method is missing an initial underscore, and so it never
gets called.

(PS: This also explains why the incorrect indentation of the `updateUi` method
doesn't raise an `AttributeError` when it's referenced in `self.connect`).

