AttributeError: 'class_c' object has no at

2019-09-14 01:20发布

I have a gui application

  1. I put text into text box and then click on the pushButton,
  2. The function callprinttext() in the moduel_b.py be called.
  3. The function callprinttext() is calling method printtext() in the moduel_c.py

but I have an error:

 AttributeError: 'class_c' object has no attribute 'lineEdit'

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))

1条回答
够拽才男人
2楼-- · 2019-09-14 01:52

Well, one way to do it is using a call back method, this way:

In 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)

Then in your calss_b method:

def callprinttext (self, txt):
    from module_c import class_c
    instance_c=class_c()
    instance_c.printtext(txt)

And Finally in your class_c method:

def printtext (self, txt):
    t=unicode(txt) 
    print t

EDIT :

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()))

And there is no need for call_back

查看更多
登录 后发表回答