Reputation: 51
The program is not working as it is supposed to. It should accept an expression and write it to the textbox above, but it isn't doing so.
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)
form = Form()
form.show()
app.exec_()
But I run window appears but when I type an expression and hit return following happens:
C:\Anaconda3\python.exe "F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw"
Traceback (most recent call last):
File "F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw", line 24, 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 "F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw", line 27, in updateUi
self.browser.append("<font color=red>%s is invalid!</font>" % text)
UnboundLocalError: local variable 'text' referenced before assignment
please help.
Upvotes: 0
Views: 50
Reputation: 244291
The problem is caused by the incompatibility between python2 and python3, for example unicode is no longer present in python3. also that there is a bad handling of the exception, for example if the error occurs in the line text = unicode (self.lineedit.text ())
then the text variable will never have been defined and therefore generates another error in the line that prints the error, considering the above I have implemented the following solution that is compatible with python2 and python3.
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):
text = str(self.lineedit.text())
try:
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)
form = Form()
form.show()
app.exec_()
Upvotes: 2