tags:

views:

15

answers:

1

Hi,

Please can someone tell me what im doing wrong here with respect to calling pwTxt.text.

#!/usr/bin/python
import sys
from PyQt4 import QtCore, QtGui

from mainwindow import Ui_MainWindow


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def on_pwExtract_pressed(self):
        print self.pwTxt.text

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

The line print self.pwTxt.text fails because it can't find the widget, pwTxt is a QLineEdit defined on the main window. I just made it in QTDesigner and generated python code with pyuic4.

How do I correctly reference other widgets on the same window, in this case I just want to get the text from a QLineEdit named pwTxt when the QPushButton pwExtract is pressed.

Thanks a lot.

+1  A: 

Try:

print self.ui.pwTxt.text()
sje397
@sje397: Thanks I had to make it .text() for it to work otherwise it outputted "<built-in method text of QLineEdit object at 0x7f8ac9675050>". Not sure why as .text should work as an attribute? All good though thanks :)
Jason
@Jason - edited to take your comment into account. `text` should be an attribute of the python object if it is a 'property' in Qt.
sje397