Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

PyQt5 QSpinBox – Deleting it according to user

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article we will see how we can delete the spin box when user command, while designing an application it could potentially consume a lot of space/memory if care is not taken when closing widgets. The QObject-based classes are designed to be (optionally) linked together in a hierarchy. When a top-level object is deleted, Qt will automatically delete all its child objects as well. In order to explicitly delete the reference of the spin box we use deleteLater method.

Syntax : spin_box.deleteLater() Argument : It takes no argument Action performed : It deletes/remove the reference of the spin box from the memory.

Implementation steps : 1. Create a spin box 2. Create a label to show spin box status 3. Create push button 4. Add action to the push button 5. Inside the action delete the reference of the spin box and update the text of label Below is the implementation 


# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python ")
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
        # calling method
        # showing all the widgets
    # method for widgets
    def UiComponents(self):
        # creating spin box
        self.spin = QSpinBox(self)
        # setting geometry to spin box
        self.spin.setGeometry(100, 100, 150, 40)
        # setting suffix to spin
        self.spin.setSuffix(" Spin Box")
        # creating label
        self.label = QLabel(self)
        # setting geometry
        self.label.setGeometry(100, 200, 300, 40)
        # setting text to the label
        self.label.setText("Spin box will delete when button get pressed")
        # creating a push button
        push = QPushButton("Press", self)
        # adding action to the push button
    # method called by push button
    def push_method(self):
        # deleting the spin box
        # showing this new name to label
        self.label.setText("Spin box is deleted")
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

My Personal Notes arrow_drop_up
Last Updated : 27 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials