AttributeError:"class_c"对象没有属性"lineEdit" [英] AttributeError: 'class_c' object has no attribute 'lineEdit'

查看:380
本文介绍了AttributeError:"class_c"对象没有属性"lineEdit"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GUI应用程序

I have a gui application

  1. 我将文本放入文本框,然后单击按钮,
  2. moduel_b.py中的函数callprinttext()被调用.
  3. 函数callprinttext()正在调用moduel_c.py
  4. 中的方法printtext()
  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

但是我有一个错误:

 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆