QtDesigner和PyQt的新手,并尝试让我的第一个简单的应用程序运行。 (用户在LineEdits中输入一到三个值,并将总和放在第四个LineEdit中)。我用Qt Designer创建了GUI(simpleAdder1.py),然后编写了以下代码(callsimpleAdder1.pyw)。不幸的是,它没有运行,代码分析表明来自simpleAdder1 import *'的问题。线。问题是......"来自simpleAdder1 import *,使用过,无法检测到未定义的名称'
我原本以为这是路径问题。但simpleAdder1.py与callsimpleadder1.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))
答案 0 :(得分:2)
好的,我想我(也许)了解你的问题。
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
然后,我建议使用"使用import"只有,像这样;
from simpleAdder1 import Ui_Dialog, QtGui
你的GUI出了什么问题?这是因为 main ;
中的这一行myapp.show
要解决问题,请"通话功能" (在旧的没有调用函数的任何东西)像这样;
myapp.show()
此致