0

i just started to learn PyQt and GUI programing and i copied this code exactly from the book "Rapid GUI Programming with Python and Qt:The Definitive Guide to PyQt Programming" and it supposed to show a calculator that calculates an expression. when i run the application main window shows up but does not do anything and since i copied the code form a well known pyqt book it's very strange. i am using python 3.4.4 and pyqt4 .this is the code i copied from book:

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("retrunPressed()"),
                     self.updateUi)
        self.setWindowTitle("Calculate")

    def updateUi(self):
        try:
            text= unicode(self.lineedit.text())
            self.browser.append("{0} = <b>{1}</b>".format(text,eval(text)))
        except:
            self.browser.append(
                "<font color=red>%s is invalid!</font>" % text)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

these are the errors i get:

Traceback (most recent call last):

File "calculator.pyw", line 25, in updateUi text = unicode(self.lineedit.text()) NameError: name 'unicode' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "calculator.pyw", line 29, in updateUi "%s is invalid!" % text) UnboundLocalError: local variable 'text' referenced before assignment

i know its not good idea to ask someone else to debug my code but i did all i could about it and nothing came up. thanks

1 Answers1

0

You have a typo in your signal name it should be

self.connect(self.lineedit, SIGNAL("returnPressed()"),

not

self.connect(self.lineedit, SIGNAL("retrunPressed()"),

so apparently you didn't copy it well, also it seems that book was written in python2 so you don't need the unicode function strings in python3 are already unicode by default

danidee
  • 9,298
  • 2
  • 35
  • 55
  • Thank you.my problem solved with unicode and signal change.can you recommand me a book which is more recent and covers pyqt5 and python3? – kourosh daryaee Jun 24 '16 at 12:47
  • unfortunately i do not know of any full pyqt5 ebooks that have been made but there are a lot of tutorials and the concepts of `Qt4` are still there but take a look at this link http://stackoverflow.com/questions/20996193/is-there-a-tutorial-specifically-for-pyqt5 – danidee Jun 24 '16 at 15:02
  • also don't forget to accept my answer if it worked for you :) – danidee Jun 24 '16 at 15:02
  • i did not know there's an accept button thanks for mentioning it. – kourosh daryaee Jun 26 '16 at 17:37