0

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))
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
Ahmad Ali
  • 57
  • 1
  • 8

1 Answers1

0

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

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • AttributeError: 'MainWindow' object has no attribute 'lineEdit' – Ahmad Ali Dec 27 '15 at 17:24
  • and ,I do not want Add functions in the main.py – Ahmad Ali Dec 27 '15 at 17:26
  • My target: Reduce the number of functions in main.py – Ahmad Ali Dec 27 '15 at 17:28
  • I need devote main.py for limited functions (signal and run application), – Ahmad Ali Dec 27 '15 at 17:49
  • @AhmadAli...did you write something in `lineEdit` before you press the `pushButton`?.. – Iron Fist Dec 27 '15 at 19:00
  • @AhmadAli...`lineEdit.text()` returns `QString`, so had to convert it to `string` using `str()`...check my updated answer – Iron Fist Dec 27 '15 at 19:08
  • The same problem, lines empty – Ahmad Ali Dec 27 '15 at 19:22
  • @AhmadAli...Check again...this should work as I've test it myself. – Iron Fist Dec 27 '15 at 19:41
  • NICE, GOOD JOB , THANKS VERY MUCH. – Ahmad Ali Dec 27 '15 at 19:47
  • Can you help me here ??? http://stackoverflow.com/questions/34386561/pyqt-not-responding – Ahmad Ali Dec 27 '15 at 19:50
  • @AhmadAli...Check my answer ..https://stackoverflow.com/questions/34386561/pyqt-not-responding/34484638#34484638 – Iron Fist Dec 27 '15 at 20:45
  • If I had lineEdit2 ,lineEdit3 .....How do I modify the last code? QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), \ lambda: instance_b.callprinttext(self.ui.lineEdit.text())) – Ahmad Ali Dec 27 '15 at 22:23
  • Can you explain more...or add it to your question? – Iron Fist Dec 28 '15 at 12:26
  • I add it here http://stackoverflow.com/questions/34499525/how-do-i-call-an-gui-object-or-more-from-main-class – Ahmad Ali Dec 28 '15 at 19:31
  • @ Iron Fist I am can not add more questions>> Message from stackoverflow.com :(you have reached your question limit) , can u help me?? – Ahmad Ali Dec 31 '15 at 10:16
  • @AhmadAli...What's your issue this time? – Iron Fist Dec 31 '15 at 17:16
  • @ Iron Fist .. Message from stackoverflow.com : You have reached your question limit Sorry, we are no longer accepting questions from this account. See the Help Center to learn more. – Ahmad Ali Dec 31 '15 at 18:09
  • @AhmadAli...It means you have asked many questions....may be you can wait for some time to be able to Post Questions again. – Iron Fist Dec 31 '15 at 18:11
  • @ Iron Fist Message : Users who are banned from asking questions see the following error message when trying to post a new question: We are no longer accepting questions from this account. See the Help Center to learn more. Question bans do not affect other privileges, such as commenting or voting, and there is no indication to the rest of the community that a particular user has been banned. How can I get out of a question ban? The ban will be lifted automatically by the system when it determines that your positive contributions outweigh those questions which were poorly received. – Ahmad Ali Dec 31 '15 at 19:02
  • @AhmadAli...It means you've asked too many questions more than your contributions as answers to SO community...so you need to answers more questions than asking them... – Iron Fist Dec 31 '15 at 19:05
  • @ Iron Fist.... Can you review my questions and Fix the problem? I need your help to ask you more question about bython. – Ahmad Ali Dec 31 '15 at 19:16