pyqt4 - 无法检测未定义的名称

标签 pyqt4 qt-designer

QtDesigner 和 PyQt 新手,正在尝试让我的第一个简单应用程序运行。 (用户在 LineEdits 中输入一到三个值,并将总和放入第四个 LineEdit 中)。我使用 Qt Designer 创建了 GUI (simpleAdder1.py),然后编写了以下代码 (callsimpleAdder1.pyw)。不幸的是,它没有运行,代码分析表明“from simpleAdder1 import *”行存在问题。问题是......“from simpleAdder1 import *,使用,无法检测未定义的名称”

我原本以为是路径问题。但 simpleAdder1.py 与 Callsimplader1.pyw 位于同一目录中。我还将 simpleAdder1 复制到 Python 检查的路径之一,但这没有帮助。

我哪里出错了?哪些名称未定义?我该如何解决这个问题?

import sys
from simpleAdder1 import *

class MyForm(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)        

    def on_v1Input_textChanged(self):
        self.ui.calcResult()        

    def on_v2Input_textChanged(self):
        self.ui.calcResult()   

    def on_v3Input_textChanged(self):
        self.ui.calcResult()   

    def calcResult(self):
        if len(self.ui.v1Input.txt())!=0:
            a=float(self.ui.v1Input.txt())
        else:
            a=0
        if len(self.ui.v2Input.txt())!=0:
            b=float(self.ui.v2Input.txt())
        else:
            b=0
        if len(self.ui.v3Input.txt())!=0:
            c=float(self.ui.v1Input.txt())
        else:
            c=0
        sum=a+b+c
        self.ui.calc_result.setText(+str(sum))       

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp=MyForm()
    myapp.show
    app.exec_()

GUI(simpleAdder1)的代码如下:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(673, 565)
        self.v1Input = QtGui.QLineEdit(Dialog)
        self.v1Input.setGeometry(QtCore.QRect(50, 70, 71, 20))
        self.v1Input.setObjectName(_fromUtf8("v1Input"))
        self.v2Input = QtGui.QLineEdit(Dialog)
        self.v2Input.setGeometry(QtCore.QRect(150, 70, 71, 20))
        self.v2Input.setObjectName(_fromUtf8("v2Input"))
        self.v3Input = QtGui.QLineEdit(Dialog)
        self.v3Input.setGeometry(QtCore.QRect(250, 70, 71, 20))
        self.v3Input.setObjectName(_fromUtf8("v3Input"))
        self.calc_result = QtGui.QLineEdit(Dialog)
        self.calc_result.setGeometry(QtCore.QRect(420, 70, 113, 20))
        self.calc_result.setObjectName(_fromUtf8("calc_result"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 50, 46, 13))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(160, 50, 46, 13))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(260, 50, 46, 13))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(450, 50, 46, 13))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(200, 230, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v1Input.clear)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v2Input.clear)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v3Input.clear)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        #QtCore.QObject.connect(self.v1Input,QtCore.SIGNAL("textChanged"),self.calcResult)
        #QtCore.QObject.connect(self.v2Input,QtCore.SIGNAL("textChanged"),self.calcResult)
        #QtCore.QObject.connect(self.v3Input,QtCore.SIGNAL("textChanged"),self.calcResult)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "Val 1", None))
        self.label_2.setText(_translate("Dialog", "Val 2", None))
        self.label_3.setText(_translate("Dialog", "Val 3", None))
        self.label_4.setText(_translate("Dialog", "Result", None))
        self.pushButton.setText(_translate("Dialog", "Clear Inputs", None))

最佳答案

好吧,我想我(也许)理解你的问题。

Unfortunately it's not running and code analysis suggests a problem at the 'from simpleAdder1 import *' line. The problem is ...."from simpleAdder1 import * ,used, unable to detect undefined names'

我看到你的代码,问题小部件无法显示,这不是这个错误吗?我认为这是你的路径文件的错误。 但是,我在 Eclipse Kepler Service Release 1 上使用 PyDev 2.8.2,它有(很少)警告未使用导入,如下所示,

Unused in wild import: QtCore
Found at: simpleAdder1

然后,我建议仅使用“used import”,如下所示;

from simpleAdder1 import Ui_Dialog, QtGui

你的 GUI 出了什么问题?这是因为 ma​​in 中的这一行;

myapp.show

要解决问题,请像这样“调用函数”(在旧版本中不是调用函数);

myapp.show()

问候,

关于pyqt4 - 无法检测未定义的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111205/

相关文章:

python - 如何在不使用pip的情况下安装pyqt5和pyqt5-tools?

linux - 终止子进程

python - 如何通过将光标放在框架内来调整框架大小?

python - 如何隐藏 PyQt4 Python 应用程序的任务栏图标?

python - 如何在 Qt Designer 中创建圆形按钮

macos - 在 macbook pro 上安装 PYQT5 后如何打开 QT Designer?

python - 为 ListView 创建切换 "Check All"复选框

python - 从 PyQt 小部件项设置和获取 "data"?

带有刻度标签的 Python PyQt4 slider

c++ - Qt Designer 中按钮的回调?