AttributeError:"class_c"对象没有属性"lineEdit" [英] AttributeError: 'class_c' object has no attribute 'lineEdit'
本文介绍了AttributeError:"class_c"对象没有属性"lineEdit"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个GUI应用程序
I have a gui application
- 我将文本放入文本框,然后单击按钮,
-
moduel_b.py
中的函数callprinttext()
被调用. - 函数
callprinttext()
正在调用moduel_c.py
中的方法
printtext()
- I put text into text box and then click on the pushButton,
- The function
callprinttext()
in themoduel_b.py
be called. - The function
callprinttext()
is calling methodprinttext()
in themoduel_c.py
但是我有一个错误:
AttributeError: 'class_c' object has no attribute 'lineEdit'
有人可以帮助我使用class_c
吗?
这是该代码:
can anyone help me with class_c
?
Here's the code for that:
module_c.py
class class_c (object):
def __init__(self, parent=None):
self.parent=parent
### I want a fix for error here
def printtext (self):
t=unicode(self.lineEdit.text())
print t
module_b.py
import sys
from GUI import Ui_MainWindow
from PyQt4 import QtCore, QtGui
class calss_b (object):
def __init__(self, parent=None):
pass
def callprinttext (self):
from module_c import class_c
instance_c=class_c()
instance_c.printtext()
main.py
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
from GUI import Ui_MainWindow
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
from module_b import calss_b
global instance_b
instance_b=calss_b(self)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), instance_b.callprinttext )
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
global myapp
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
GUI
# -*- coding: utf-8 -*-
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_MainWindow(object):
def setupUi(self, MainWindow):
super(Ui_MainWindow, self).__init__()
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(340, 110, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(390, 240, 151, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
推荐答案
好的,一种方法是使用回调方法,
Well, one way to do it is using a call back method, this way:
在main.py
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.call_back)
def call_back(self):
txt = str(self.ui.lineEdit.text()) #lineEdit.text() returns QString Object so we need to convert it to String object
instance_b.callprinttext(txt)
然后在您的calss_b
方法中:
def callprinttext (self, txt):
from module_c import class_c
instance_c=class_c()
instance_c.printtext(txt)
最后在您的class_c
方法中:
def printtext (self, txt):
t=unicode(txt)
print t
如果您不想添加任何回叫方法,请使用lambda
这样做,方法是:
If you don't want to add any call back method, then do it using lambda
, this way:
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), \
lambda: instance_b.callprinttext(self.ui.lineEdit.text()))
并且不需要call_back
这篇关于AttributeError:"class_c"对象没有属性"lineEdit"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文