Question

I'm trying to make it so that when I click the button, the text input field is printed to the console. Why do I keep getting an error? I'm not quite sure what I'm doing wrong.

import sys
import os
from PySide import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))

        self.setToolTip('This is a <b>QWidget</b> widget')

        # EditText Field
        labelProjectName = QtGui.QLabel('Project Name:', self)
        labelProjectName.move(15, 10)

        etProjectName = QtGui.QLineEdit('', self)
        etProjectName.resize(etProjectName.sizeHint())
        etProjectName.move(90, 7)

        # Button UI
        btn = QtGui.QPushButton('Create Folder', self)
        btn.setToolTip('This creates the folders.')
        btn.resize(btn.sizeHint())
        btn.move(5, 30)       
        btn.clicked.connect(self.generateFolders)

        self.resize(250, 150)
        self.center()

        self.setWindowTitle('Folder Utility')    
        self.show()

    def center(self):

        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def generateFolders(self):
        var = self.etProjectName.text()
        print var

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':

    main()
Was it helpful?

Solution

You need to make etProjectName an attribute of your Example class:

    self.etProjectName = QtGui.QLineEdit('', self)
    self.etProjectName.resize(self.etProjectName.sizeHint())
    self.etProjectName.move(90, 7)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top