pythonpyqtpyqt5qpushbuttonqlineedit

Clear Button PyQt5


I have been trying to build a simple GUI with:

My issue is in the second one. I have been trying to solve it by looking at solutions online but they weren't really useful so far. Can anyone give a hint on how to proceed?

Here is my code:

import sys
from PyQt5.QtWidgets import QWidget, QLineEdit
from PyQt5.QtWidgets import QLabel, QPushButton, QApplication
from PyQt5.QtCore import pyqtSlot


app = QApplication(sys.argv)

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'MyApp'
        self.left = 10
        self.top = 10
        self.width = 800
        self.height = 800
        self.initUI()
        self.show()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox for index number 1
        self.nameLabel = QLabel(self)
        self.nameLabel.setText('Insert something:')
        self.nameLabel.move(20, 80)

        self.textbox_index1 = QLineEdit(self)
        self.textbox_index1.move(20, 100)
        self.textbox_index1.resize(280, 40)

        # Create a button in the window
        self.buttonC1 = QPushButton('Clear', self)
        self.buttonC1.move(300, 119)

        # connect buttons "CLEAR" to function
        self.buttonC1.clicked.connect(self.on_clickC1)


        @pyqtSlot()
    # Functions for the CLEAR buttons
    def on_clickC1(self):
        self.x1 = clearSearch1(self.textbox_index1.text(''))
        return self.x1

    def clearSearch1(self.x):
        return self.x.clear()

if __name__ == '__main__':
    app.aboutToQuit.connect(app.deleteLater)
    ex = App()
    sys.exit(app.exec_())

Thanks so much in advance,

Mattia


Solution

  • I do not understand what you are trying to do, the solution is simple, you must connect the clicked signal to the clear method directly without creating any other function:

    class App(QWidget):
        def __init__(self):
            super().__init__()
            self.title = 'MyApp'
            self.left, self.top, self.width, self.height = 10, 10, 800, 800
            self.initUI()
            self.show()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
            # Create textbox for index number 1
            self.nameLabel = QLabel(self)
            self.nameLabel.setText('Insert something:')
            self.nameLabel.move(20, 80)
    
            self.textbox_index1 = QLineEdit(self)
            self.textbox_index1.move(20, 100)
            self.textbox_index1.resize(280, 40)
    
            # Create a button in the window
            self.buttonC1 = QPushButton('Clear', self)
            self.buttonC1.move(300, 119)
    
            # connect buttons "CLEAR" to function
            self.buttonC1.clicked.connect(self.textbox_index1.clear)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())