0
프로그램이 정상적으로 작동하지 않습니다. 표현식을 받아 들여 위의 텍스트 상자에 써야하지만 그렇게하지는 않습니다.PyQt4 프로그램이 작동하지 않습니다.
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_()
는하지만 창이 나타납니다 실행하지만 표현하고 다음 엔터를 입력 할 때 발생합니다
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
이 도움을 주시기 바랍니다.
... 정말 고마워요. 나는 실제로 같은 것을 생각했지만 v2와 v.3의 유니 코드 호환성에 대해서는 아무것도 몰랐습니다. 텍스트에 관해서는 pyqt4에 대해 아무것도 몰랐기 때문에 나는 정말로 혼란 스러웠습니다. 이 예제는 Mark Summerfield의 저서입니다. 그래서, 만약 그와 같은 사람이 잘못하면 나 같은 초심자가 아무 것도 모른다는 것입니다 .... 고맙습니다. – user7360021