2017-12-20 7 views
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_() 

enter image description here

는하지만 창이 나타납니다 실행하지만 표현하고 다음 엔터를 입력 할 때 발생합니다

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 

이 도움을 주시기 바랍니다.

답변

2

이 문제는 python2와 python3 사이의 비 호환성으로 인해 발생합니다. 예를 들어, unicode는 더 이상 python3에 없습니다. 또한 예를 들어, 오류가 행 text = unicode (self.lineedit.text())에서 발생하면 텍스트 변수가 정의되지 않았으므로 오류를 인쇄하는 줄에 다른 오류가 발생하는 등 예외 처리가 잘못되었습니다. python2 및 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_() 
+0

... 정말 고마워요. 나는 실제로 같은 것을 생각했지만 v2와 v.3의 유니 코드 호환성에 대해서는 아무것도 몰랐습니다. 텍스트에 관해서는 pyqt4에 대해 아무것도 몰랐기 때문에 나는 정말로 혼란 스러웠습니다. 이 예제는 Mark Summerfield의 저서입니다. 그래서, 만약 그와 같은 사람이 잘못하면 나 같은 초심자가 아무 것도 모른다는 것입니다 .... 고맙습니다. – user7360021

관련 문제