PyQt4 Python Gui Coding:请求帮助调试语法错误

时间:2013-11-22 21:47:20

标签: python python-2.7 user-interface pyqt4

我正在学习PyQt4并编写了一个简单的表达式求值程序。我无法调试 init 函数的第二行到第二行(self.connect .....)。 Python返回'语法错误',但我无法发现问题。我很感激您提供的任何帮助。我在Ubuntu 13.10 Linux 64位上使用Python 2.7.6。我知道空白格式稍微偏离,这是我剪切和粘贴的工件,但代码是完全正确的,因为我正在运行它。

<pre> <code>
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__(tparent)
    self.browser = QTextBrowser()
    self.lineedit = QLineEdit("Type an epression and press enter")
    self.lineedit.selectAll()
    layout = QVBoxLayout()
    layout.addWidget(self.browser)
    layout.addWidget(self.lineedit)
    self.setLayout(layout)
    self.lineedit(self.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_()

1 个答案:

答案 0 :(得分:0)

你错过了上面一行的关闭

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