Home>

The window contains a block of buttons with random names.
When you click on the "X " button, you need to delete only these buttons, and all other buttons and form elements should remain on the screen.

Logic suggests that you need to combine such buttons (which need to be removed) through a common identifier, and then remove buttons that have this identifier set on the form.
But I can't figure out how to do it.

A small example:

import sys
import random
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
class MainWindow (QtWidgets.QMainWindow):
    def __init __ (self):
        super () .__ init __ ()
        self.setWindowTitle ("Example")
        buttons= [str (random.randint (0,100)), str (random.randint (0,100)), str (random.randint (0,100)), str (random.randint (0,100))]
        x= 10
        y= 0
        # A block of buttons that must be deleted by pressing the "X" button
        for button in buttons:
            y= y + 25
            btn= QPushButton (button, self)
            btn.move (x, y)
            btn.setToolTip (button)
            btn.resize (btn.sizeHint ())
        # Buttons that should remain after clicking on "X"
        btn= QPushButton ("This button should stay", self)
        btn.move (100, 40)
        btn= QPushButton ("And this button should stay", self)
        btn.move (100, 80)
        btn= QPushButton ("X", self)
        btn.move (100, 10)
        btn.clicked.connect (self.delete)
    def delete (self):
        reply= QMessageBox.question (self, 'Message', "Are you sure you want to delete?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        print ("Yes" if reply== QMessageBox.Yes else "No")
if __name__== '__main__':
    app= QtWidgets.QApplication (sys.argv)
    w= MainWindow ()
    w.show ()
    sys.exit (app.exec_ ())

A window appears on the screen and in it, when you click on X , you only need to delete buttons with random names (in my example, these are random numbers), and leave the rest in place.

  • Answer # 1

    I saw your task like this:

    import sys
    import random
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import *
    class MainWindow (QtWidgets.QMainWindow):
        def __init __ (self):
            super () .__ init __ ()
            self.setWindowTitle ("Example")
            self.centralWidget= QWidget (self) # !!!
            self.setCentralWidget (self.centralWidget) # !!!
            self.addButtons () # +++
            # Buttons that should remain after clicking on "X"
            btn= QPushButton ("This button should stay", self.centralWidget)
            btn.move (100, 40)
            btn= QPushButton ("And this button should stay", self.centralWidget)
            btn.move (100, 80)
            self.btn_add_delete= QPushButton ("Delete", self.centralWidget) # self.centralWidget
            self.btn_add_delete.move (100, 10)
            self.btn_add_delete.clicked.connect (self.deleteButtons)
        def addButtons (self):
            self.buttons= [
                QPushButton (str (random.randint (0,100))),
                QPushButton (str (random.randint (0,100))),
                QPushButton (str (random.randint (0,100))),
                QPushButton (str (random.randint (0,100)))
            ]
            x= 10
            y= 0
            # A block of buttons that must be deleted by clicking on the "Delete" button
            for button in self.buttons:
                y= y + 25
                btn= button
                btn.setParent (self.centralWidget)
                btn.move (x, y)
                btn.setToolTip (button.text ())
                btn.resize (btn.sizeHint ())
                btn.show ()
        def deleteButtons (self):
            if self.btn_add_delete.text ()== "Delete":
                reply= QMessageBox.question (
                    self,
                    'Message', "Are you sure you want to delete?",
                    QMessageBox.Yes | QMessageBox.No,
                    QMessageBox.No
                )
                if reply== QMessageBox.Yes:
                    print ("Yes <
    <
    -----")
                    for button in self.buttons:
                        button.hide ()
                    self.btn_add_delete.setText ("Add")
            elif self.btn_add_delete.text ()== "Add":
                self.addButtons ()
                self.btn_add_delete.setText ("Delete")
    if __name__== '__main__':
        app= QtWidgets.QApplication (sys.argv)
        w= MainWindow ()
        w.resize (400, 200)
        w.show ()
        sys.exit (app.exec_ ())
    
  • Answer # 2

    This is how you can:

    import sys
    import random
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import *
    class MainWindow (QtWidgets.QMainWindow):
        def __init __ (self):
            super () .__ init __ ()
            self.setWindowTitle ("Example")
            # -----------------------------
            button_names= [str (random.randint (0,100)), str (random.randint (0,100)), str (random.randint (0,100)), str (random.randint (0,100))]
            self.buttons= []
            # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            x= 10
            y= 0
            # A block of buttons that must be deleted by pressing the "X" button
            for name in button_names: # <
    -----
                y= y + 25
                btn= QPushButton (name, self)
                btn.move (x, y)
                btn.setToolTip (name)
                btn.resize (btn.sizeHint ())
                self.buttons.append (btn) # <
    -----
            # Buttons that should remain after clicking on "X"
            btn= QPushButton ("This button should stay", self)
            btn.move (100, 40)
            btn= QPushButton ("And this button should stay", self)
            btn.move (100, 80)
            btn= QPushButton ("X", self)
            btn.move (100, 10)
            btn.clicked.connect (self.delete)
        def delete (self):
            reply= QMessageBox.question (self, 'Message', "Are you sure you want to delete?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            print ("Yes" if reply== QMessageBox.Yes else "No")
            # --------------------------
            if reply== QMessageBox.Yes:
                for btn in self.buttons:
                    btn.hide ()
            # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    if __name__== '__main__':
        app= QtWidgets.QApplication (sys.argv)
        w= MainWindow ()
        w.show ()
        sys.exit (app.exec_ ())
    

    @PetrSklyarov think not

    n1tr0xs2021-02-23 13:06:43