简体   繁体   中英

How to delete a button using PYQT4?

I am trying to delete this button

btn = QtGui.QPushButton("Log in", self)
btn.clicked.connect(self.remove)

When the button is clicked, it should disappear,but this doesn't work

btn.deleteQPushButton()

Or if there's any way to entirely delete every button in the window?

An elegant solution is to use deleteLater() :

btn = QtGui.QPushButton("Log in", self)
btn.clicked.connect(btn.deleteLater)

Yep, use deleteLater

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton

if __name__ == '__main__':

    app = QApplication(sys.argv)

    wMain = QWidget()
    wMain.resize(250, 150)
    wMain.setWindowTitle('Main')
    wMain.show()

    centralLayout = QHBoxLayout(wMain)
    wMain.setLayout(centralLayout)

    button = QPushButton('Delete me',wMain)
    button.clicked.connect(button.deleteLater)
    centralLayout.addWidget(button)

    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM