PyQt4 name in function not defined

Following code Shows a widget with a lineedit and a button:

import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel, QComboBox, QProgressBar, QFileDialog
from PyQt4.QtCore import QSize, pyqtSlot

class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()
        self.setGeometry(500, 300, 820, 350)
        self.setWindowTitle("Widget")
        self.initUI()

    def initUI(self):

        #Buttons
        btnposx = 30
        btnposy = 50

        btn4 = QPushButton('Button 4', self)
        btn4.move(btnposx,btnposy+220)
        btn4.clicked.connect(self.le1_get)


        #LineEdits
        leposx = 150
        leposy = btnposy

        le1 = QLineEdit(self)
        le1.move(leposx,leposy)
        le1.resize(630,26)

        self.show()

    @pyqtSlot()
    def le1_get(self):
        le1inp = self.le1.text()
        print(le1inp)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Spyder shows that the Name of le1 (in the function) is not defined. But it is in the above function.

enter image description here

Output:

le1inp = self.le1.text()
AttributeError: 'App' object has no attribute 'le1'

Upvotes: 0

Views: 233

Answers (1)

strubbly
strubbly

Reputation: 3477

This is simply a Python error - nothing to do with PyQt. In this line you try to find an instance attribute le1 of the App type:

le1inp = self.le1.text()

As the error message says, you have not created such an attribute. Python maintains a syntactic distinction between attributes and locals. You have defined a local earlier (now out of scope) with a similar name that is presumably supposed to be the required instance attribute. Just change the code to:

self.le1 = QLineEdit(self)
self.le1.move(leposx,leposy)
self.le1.resize(630,26)

and that should fix this problem. You probably want to do the same with the button.

Upvotes: 1

Related Questions