英文:
Pyqt5 how can I remove Qlabel after adding them several times
问题
我正在尝试制作一款卡片游戏。在进行了1局游戏后,我需要清除QLabel
以开始新游戏。以下是我正在调试的摘要。
我想使用QPushButton
操作来清除所有的QLabel
。但实际上我只能移除最后一个QLabel
。其余的QLabel
仍然保留。
我如何移除所有的QLabel
?
class Main(QMainWindow):
def __init__(self):
super().__init__()
# 整体布局
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("添加卡片")
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
remove_bt = QPushButton("移除卡片")
remove_bt.setStyleSheet("background-color:pink")
bt1_layout.addWidget(remove_bt,3,0)
remove_bt.clicked.connect(self.card_removal)
# 整体布局
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()
注意:我已经移除了HTML实体编码(如"
)以及修复了self.card_w6
的全局引用。
英文:
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()
答案1
得分: -1
创建一个名为self.cards
的QLabels列表,如下所示:self.cards = []
,然后将self.cards.append(card_w6)
添加到其中。
> QObject::deleteLater()
>安排此对象进行删除。
>当控制返回到事件循环时,对象将被删除。
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()
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)
# 用于卡片移除的按钮 调试目的
remove_bt = QPushButton("card remove")
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())
英文:
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论