如何在多次添加后删除QLabel。

huangapple go评论109阅读模式
英文:

Pyqt5 how can I remove Qlabel after adding them several times

问题

我正在尝试制作一款卡片游戏。在进行了1局游戏后,我需要清除QLabel以开始新游戏。以下是我正在调试的摘要。

我想使用QPushButton操作来清除所有的QLabel。但实际上我只能移除最后一个QLabel。其余的QLabel仍然保留。

我如何移除所有的QLabel

  1. class Main(QMainWindow):
  2. def __init__(self):
  3. super().__init__()
  4. # 整体布局
  5. layout = QVBoxLayout()
  6. self.setStyleSheet("background-color:white")
  7. self.card_layout6 = QHBoxLayout()
  8. self.card_w6 = QLabel()
  9. self.card_w6.setPixmap(QPixmap("card.bmp"))
  10. self.card_layout6.addWidget(self.card_w6)
  11. layout.addLayout(self.card_layout6)
  12. bt1_layout = QtWidgets.QGridLayout()
  13. bt1_layout.setContentsMargins(0,0,0,20)
  14. layout.addLayout(bt1_layout)
  15. self.setGeometry(500, 100, 330, 200)
  16. height = 800
  17. width = 800
  18. self.setFixedHeight(height)
  19. self.setFixedWidth(width)
  20. self.request_bt = QPushButton("添加卡片")
  21. self.request_bt.setStyleSheet("background-color:lightblue")
  22. self.request_bt.clicked.connect(self.add_card_layout6)
  23. bt1_layout.addWidget(self.request_bt,0,0)
  24. # 用于卡片移除的PushButton
  25. remove_bt = QPushButton("移除卡片")
  26. remove_bt.setStyleSheet("background-color:pink")
  27. bt1_layout.addWidget(remove_bt,3,0)
  28. remove_bt.clicked.connect(self.card_removal)
  29. # 整体布局
  30. self.container = QWidget()
  31. self.container.setLayout(layout)
  32. self.setCentralWidget(self.container)
  33. self.show()
  34. def add_card_layout6(self):
  35. global card_w6
  36. self.card_w6 = QLabel()
  37. self.card_w6.setPixmap(QPixmap("card.bmp"))
  38. self.card_layout6.addWidget(self.card_w6)
  39. def card_removal(self):
  40. 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?

  1. class Main(QMainWindow):
  2. def __init__(self):
  3. super().__init__()
  4. #overall layout
  5. layout = QVBoxLayout()
  6. self.setStyleSheet("background-color:white")
  7. self.card_layout6 = QHBoxLayout()
  8. self.card_w6 = QLabel()
  9. self.card_w6.setPixmap(QPixmap("card.bmp"))
  10. self.card_layout6.addWidget(self.card_w6)
  11. layout.addLayout(self.card_layout6)
  12. bt1_layout = QtWidgets.QGridLayout()
  13. bt1_layout.setContentsMargins(0,0,0,20)
  14. layout.addLayout(bt1_layout)
  15. self.setGeometry(500, 100, 330, 200)
  16. height = 800
  17. width = 800
  18. self.setFixedHeight(height)
  19. self.setFixedWidth(width)
  20. self.request_bt = QPushButton("add card")
  21. self.request_bt.setStyleSheet("background-color:lightblue")
  22. self.request_bt.clicked.connect(self.add_card_layout6)
  23. bt1_layout.addWidget(self.request_bt,0,0)
  24. # Pushbutton for card removal
  25. remove_bt = QPushButton("card reomve")
  26. remove_bt.setStyleSheet("background-color:pink")
  27. bt1_layout.addWidget(remove_bt,3,0)
  28. remove_bt.clicked.connect(self.card_removal)
  29. #overall layout
  30. self.container = QWidget()
  31. self.container.setLayout(layout)
  32. self.setCentralWidget(self.container)
  33. self.show()
  34. def add_card_layout6(self):
  35. global card_w6
  36. self.card_w6 = QLabel()
  37. self.card_w6.setPixmap(QPixmap("card.bmp"))
  38. self.card_layout6.addWidget(self.card_w6)
  39. def card_removal(self):
  40. self.card_w6.clear()

答案1

得分: -1

创建一个名为self.cards的QLabels列表,如下所示:self.cards = [],然后将self.cards.append(card_w6)添加到其中。

> QObject::deleteLater()
>安排此对象进行删除。
>当控制返回到事件循环时,对象将被删除。

  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. from PyQt5.Qt import *
  4. class Main(QMainWindow):
  5. def __init__(self):
  6. super().__init__()
  7. self.container = QWidget()
  8. self.setCentralWidget(self.container)
  9. self.setStyleSheet("background-color: blue")
  10. self.card_w6 = QLabel()
  11. self.card_w6.setPixmap(QPixmap("lena.jpg").scaled(200, 200))
  12. self.card_layout6 = QGridLayout()
  13. self.card_layout6.addWidget(self.card_w6, 0, 0)
  14. self.card_layout6.setColumnStretch(33, 1)
  15. layout = QVBoxLayout(self.container)
  16. layout.addLayout(self.card_layout6)
  17. bt1_layout = QtWidgets.QGridLayout()
  18. bt1_layout.setContentsMargins(0, 0, 0, 20)
  19. layout.addLayout(bt1_layout)
  20. self.request_bt = QPushButton("add card")
  21. self.request_bt.setStyleSheet("background-color:lightblue")
  22. self.request_bt.clicked.connect(self.add_card_layout6)
  23. bt1_layout.addWidget(self.request_bt, 0, 0)
  24. # 用于卡片移除的按钮 调试目的
  25. remove_bt = QPushButton("card remove")
  26. remove_bt.setStyleSheet("background-color:pink")
  27. bt1_layout.addWidget(remove_bt, 3, 0)
  28. remove_bt.clicked.connect(self.card_removal)
  29. self.cards = [] # +++
  30. self.i = 1 # +++
  31. def add_card_layout6(self):
  32. # ? global card_w6
  33. card_w6 = QLabel()
  34. card_w6.setPixmap(QPixmap("Ok.png").scaled(135, 170))
  35. self.card_layout6.addWidget(card_w6, 0, self.i)
  36. self.i += 1
  37. self.cards.append(card_w6)
  38. def card_removal(self):
  39. # self.card_w6.clear()
  40. for card in self.cards: # +++
  41. card.deleteLater() # +++
  42. self.cards = [] # +++
  43. self.i = 1
  44. if __name__ == "__main__":
  45. app = QApplication(sys.argv)
  46. window = Main()
  47. window.resize(800, 600)
  48. window.show()
  49. sys.exit(app.exec())

如何在多次添加后删除QLabel。

英文:

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.

  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. from PyQt5.Qt import *
  4. class Main(QMainWindow):
  5. def __init__(self):
  6. super().__init__()
  7. self.container = QWidget()
  8. self.setCentralWidget(self.container)
  9. self.setStyleSheet("background-color: blue")
  10. self.card_w6 = QLabel()
  11. self.card_w6.setPixmap(QPixmap("lena.jpg").scaled(200, 200))
  12. self.card_layout6 = QGridLayout() # + QGridLayout
  13. self.card_layout6.addWidget(self.card_w6, 0, 0)
  14. self.card_layout6.setColumnStretch(33, 1) # +
  15. layout = QVBoxLayout(self.container)
  16. layout.addLayout(self.card_layout6)
  17. bt1_layout = QtWidgets.QGridLayout()
  18. bt1_layout.setContentsMargins(0, 0, 0, 20)
  19. layout.addLayout(bt1_layout)
  20. self.request_bt = QPushButton("add card")
  21. self.request_bt.setStyleSheet("background-color:lightblue")
  22. self.request_bt.clicked.connect(self.add_card_layout6)
  23. bt1_layout.addWidget(self.request_bt, 0, 0)
  24. # Pushbutton for card removal ####debugging purpose
  25. remove_bt = QPushButton("card reomve")
  26. remove_bt.setStyleSheet("background-color:pink")
  27. bt1_layout.addWidget(remove_bt, 3, 0)
  28. remove_bt.clicked.connect(self.card_removal)
  29. self.cards = [] # +++
  30. self.i = 1 # +++
  31. def add_card_layout6(self):
  32. # ? global card_w6
  33. card_w6 = QLabel()
  34. card_w6.setPixmap(QPixmap("Ok.png").scaled(135, 170))
  35. self.card_layout6.addWidget(card_w6, 0, self.i) # +
  36. self.i += 1 # +
  37. self.cards.append(card_w6) # +
  38. def card_removal(self):
  39. # self.card_w6.clear()
  40. for card in self.cards: # +++
  41. card.deleteLater() # +++
  42. self.cards = [] # +++
  43. self.i = 1
  44. if __name__ == "__main__":
  45. app = QApplication(sys.argv)
  46. window = Main()
  47. window.resize(800, 600)
  48. window.show()
  49. sys.exit(app.exec())

如何在多次添加后删除QLabel。

huangapple
  • 本文由 发表于 2023年6月11日 21:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定