I have a gui application
- 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
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))
Well, one way to do it is using a call back method, this way:
In
main.py
Then in your
calss_b
method:And Finally in your
class_c
method:EDIT :
If you don't want to add any call back method, then do it using
lambda
, this way:And there is no need for
call_back