-1

Am trying to use below code but when executed it say "Python has stopped working" and it restarts the shell. Basically am trying to read the text from QLineEdit (user input) value when the button is clicked, but it fails.

class Display(QWidget):    
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        search_dir_label = QLabel('Directory to Search')
        search_dir_te = QLineEdit()
        search_dir_layout = QHBoxLayout(self)
        search_dir_layout.addWidget(search_dir_label)
        search_dir_layout.addWidget(search_dir_te)
        vert_layout1.addLayout(search_dir_layout)

        search_button = QPushButton('Search')
        search_button.clicked.connect(self.sendval)   
        cancel_button = QPushButton('Cancel')
        search_cancel_layout = QHBoxLayout(self)
        search_cancel_layout.addWidget(search_button)
        search_cancel_layout.addWidget(cancel_button)
        search_cancel_layout.setAlignment(Qt.AlignCenter)
        vert_layout1.addLayout(search_cancel_layout)

    def sendval(self):
        print(self.search_dir_te.text)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
s3-89
  • 75
  • 1
  • 8

1 Answers1

0

You just need to change search_dir_te to self.search_dir_te in initUI() so it can be accessed in other functions. Also, add the parenthesis to call the method text() in sendval()

def initUI(self):
    search_dir_label = QLabel('Directory to Search')
    self.search_dir_te = QLineEdit()
    search_dir_layout = QHBoxLayout(self)
    search_dir_layout.addWidget(search_dir_label)
    search_dir_layout.addWidget(self.search_dir_te)
    vert_layout1.addLayout(search_dir_layout)

    search_button = QPushButton('Search')
    search_button.clicked.connect(self.sendval)   
    cancel_button = QPushButton('Cancel')
    search_cancel_layout = QHBoxLayout(self)
    search_cancel_layout.addWidget(search_button)
    search_cancel_layout.addWidget(cancel_button)
    search_cancel_layout.setAlignment(Qt.AlignCenter)
    vert_layout1.addLayout(search_cancel_layout)

def sendval(self):
    print(self.search_dir_te.text())
alec
  • 5,799
  • 1
  • 7
  • 20