2017-12-20 16 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とv3との互換性については何も知らなかったのです。テキストのことについては、私はpyqt4について何も知らなかったので、私は本当に混乱しました。この例はMark Summerfieldの本からのものです。だから、もし彼のような人が間違っていたら、私のような初心者は何も知りません... ....ありがとう – user7360021

関連する問題