MrGal
MrGal

Reputation: 19

Delete Label and Button on Button Click PyQt5

I'm writing a program that has a list showing current tasks. Simplified version look something like this:

tasklist

Then I click the add button it has to append the task to the other one, and then I click X it has to delete the task. I'm not that advanced in programming and can't figure out how to do it right. The code look like this:

import sys
from PyQt5.QtWidgets import (
    QApplication,
    QGridLayout,
    QLineEdit,
    QPushButton,
    QVBoxLayout,
    QWidget,
    QLabel)


class App(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.taskList = ['Some text 1', 'Some text 2', 'some text 3']

        self.initUI()

    def initUI(self):
        self.title = 'Task list'
        self.main_layout = QVBoxLayout()
        self.grid = QGridLayout()
        self.input_field = QLineEdit()
        self.add = QPushButton('add')
        self.add.clicked.connect(self.addTask)
        self.grid.addWidget(self.input_field, 0, 0)
        self.grid.addWidget(self.add, 0, 1)

        self.showTasks()

        self.main_layout.addLayout(self.grid)
        self.main_layout.addStretch()

        self.setLayout(self.main_layout)
        self.setWindowTitle(self.title)
        self.resize(300, 100)
        self.show()

    def showTasks(self):
        row = 2
        col = 0
        for task in self.taskList:
            task_label = QLabel(task)
            task_button = QPushButton('X')
            task_button.clicked.connect(
                lambda: self.removeTask(task_label.text()))
            self.grid.addWidget(task_label, row, col, 1, 1)
            self.grid.addWidget(task_button, row, col+1, 1, 1)
            row += 1

    def addTask(self):
        task = self.input_field.text()
        if task:
            self.taskList.append(task)
        self.update()

    def removeTask(self, task):
        self.taskList.remove(task)
        self.update()


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

Upvotes: 0

Views: 920

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

A possible solution is to map the buttons and labels in a dictionary, getting the button through the sender method and then deleting them using deleteLater.

class App(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.taskList = ["Some text 1", "Some text 2", "some text 3"]

        self.initUI()

    def initUI(self):
        self.title = "Task list"
        self.main_layout = QVBoxLayout()
        self.grid = QGridLayout()
        self.input_field = QLineEdit()
        self.add = QPushButton("add")
        self.add.clicked.connect(self.handle_add_clicked)
        self.grid.addWidget(self.input_field, 0, 0)
        self.grid.addWidget(self.add, 0, 1)

        self.mapping_widget = {}

        self.showTasks()

        self.main_layout.addLayout(self.grid)
        self.main_layout.addStretch()

        self.setLayout(self.main_layout)
        self.setWindowTitle(self.title)
        self.resize(300, 100)
        self.show()

    def add_task(self, task):
        row = self.grid.rowCount()
        task_label = QLabel(task)
        task_button = QPushButton("X")
        task_button.clicked.connect(self.handle_delete_clicked)
        self.grid.addWidget(task_label, row, 0)
        self.grid.addWidget(task_button, row, 1)
        self.mapping_widget[task_button] = task_label

    def showTasks(self):
        for task in self.taskList:
            self.add_task(task)

    def handle_add_clicked(self):
        task = self.input_field.text()
        self.add_task(task)

    def handle_delete_clicked(self):
        button = self.sender()
        if not isinstance(button, QPushButton):
            return

        label = self.mapping_widget.get(button)
        if label is None:
            return

        del self.mapping_widget[button]
        button.deleteLater()
        label.deleteLater()

Upvotes: 2

Related Questions