1

I am trying to make a card game. After 1 game, I need to clear QLabel to start new game.
Following is the summery that I am debugging.

I want to clear all QLabel with QPushButton operation. But actually I could remove only the last Qlabel. Rest of QLabel still stay.
How Can I remove all QLabel?

class Main(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        #overall layout
        layout = QVBoxLayout()
        self.setStyleSheet("background-color:white")                
        self.card_layout6 = QHBoxLayout()   
        
        self.card_w6 = QLabel() 
        self.card_w6.setPixmap(QPixmap("card.bmp"))
        self.card_layout6.addWidget(self.card_w6)
        layout.addLayout(self.card_layout6)
        
        bt1_layout = QtWidgets.QGridLayout()
        bt1_layout.setContentsMargins(0,0,0,20)
        layout.addLayout(bt1_layout)
        self.setGeometry(500, 100, 330, 200)  
        
        height = 800
        width = 800
        self.setFixedHeight(height)
        self.setFixedWidth(width)
        
        self.request_bt = QPushButton("add card")
        self.request_bt.setStyleSheet("background-color:lightblue")        
        self.request_bt.clicked.connect(self.add_card_layout6)
        bt1_layout.addWidget(self.request_bt,0,0)
        
        
        # Pushbutton for card removal 
        remove_bt = QPushButton("card reomve")
        remove_bt.setStyleSheet("background-color:pink")        
        bt1_layout.addWidget(remove_bt,3,0)
        remove_bt.clicked.connect(self.card_removal)
        
        #overall layout        
        self.container = QWidget()
        self.container.setLayout(layout)
        self.setCentralWidget(self.container)       
        self.show()
        
    def add_card_layout6(self):  
        global card_w6
        self.card_w6 = QLabel() 
        self.card_w6.setPixmap(QPixmap("card.bmp"))
        self.card_layout6.addWidget(self.card_w6)
     
    def card_removal(self):
         self.card_w6.clear()
            
S. Nick
  • 12,879
  • 8
  • 25
  • 33
Taniguchi
  • 13
  • 4
  • Can you post the content of your method `self.card_removal`? – Buzz Jun 11 '23 at 13:36
  • QLabels objects are referenced in `card_layout6`. You can loop through each item and remove them – yoonghm Jun 11 '23 at 13:41
  • def card_removal updated. (sorry, code was lost.) – Taniguchi Jun 11 '23 at 13:58
  • Don't use the same `self.card_w6` as a reference whenever you create a label, otherwise the previous reference will be lost. Instead, use a list as instance member (eg. `self.cards = []`) and `append()` a card every time you create one, then it's just a matte of iterating the list when you want to clear them. – musicamante Jun 11 '23 at 14:39

1 Answers1

-1

Create a list of your QLabels like self.cards = [] and add self.cards.append(card_w6) to it.

QObject::deleteLater()
Schedules this object for deletion.
The object will be deleted when control returns to the event loop.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *


class Main(QMainWindow):
    def __init__(self):
        super().__init__()
       
        self.container = QWidget()
        self.setCentralWidget(self.container)    
        self.setStyleSheet("background-color: blue") 
        
        self.card_w6 = QLabel() 
        self.card_w6.setPixmap(QPixmap("lena.jpg").scaled(200, 200))
        
        self.card_layout6 = QGridLayout()                           # + QGridLayout
        self.card_layout6.addWidget(self.card_w6, 0, 0)
        self.card_layout6.setColumnStretch(33, 1)                   # +
        
        layout = QVBoxLayout(self.container)
        layout.addLayout(self.card_layout6)

        bt1_layout = QtWidgets.QGridLayout()
        bt1_layout.setContentsMargins(0, 0, 0, 20)
        layout.addLayout(bt1_layout)
    
        self.request_bt = QPushButton("add card")
        self.request_bt.setStyleSheet("background-color:lightblue")        
        self.request_bt.clicked.connect(self.add_card_layout6)
        bt1_layout.addWidget(self.request_bt, 0, 0)
        
        # Pushbutton for card removal  ####debugging purpose        
        remove_bt = QPushButton("card reomve")
        remove_bt.setStyleSheet("background-color:pink")        
        bt1_layout.addWidget(remove_bt, 3, 0)
        remove_bt.clicked.connect(self.card_removal)
        
        self.cards = []                                              # +++
        self.i = 1                                                   # +++
        
    def add_card_layout6(self):  
# ?      global card_w6
        card_w6 = QLabel() 
        card_w6.setPixmap(QPixmap("Ok.png").scaled(135, 170))
        
        self.card_layout6.addWidget(card_w6, 0, self.i)               # +
        self.i += 1                                                   # +
        self.cards.append(card_w6)                                    # +
        
    def card_removal(self):
#        self.card_w6.clear()

        for card in self.cards:                                       # +++
            card.deleteLater()                                        # +++
        self.cards = []                                               # +++
        self.i = 1
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Main()
    window.resize(800, 600)
    window.show()
    sys.exit(app.exec())        

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • >S.Nick Thank you. I confirmed the function. that helped me a lot. – Taniguchi Jun 13 '23 at 14:00
  • @Taniguchi If my answer helped you then do not forget to mark as correct, if you do not know how to do it then check the https://stackoverflow.com/tour – S. Nick Jun 13 '23 at 14:17
  • S,Nick thanks for comment. still I cannot find where to push or select to mark your answer as correct. let me check bit more. if you know, please give me direction. – Taniguchi Jun 14 '23 at 22:37
  • I marked you answer as "accepted" This is the what you requested I think. Regards – Taniguchi Jun 15 '23 at 12:31