I am testing the updating of Qlabel. I basically have question displaying in random order, but the old text of the label still displays and combines with the new text. I am not sure how to clear it out between 'OK' clicks.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import random
class GameDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QGridLayout()
lblWBS = QLabel("lblWBS")
lblDialog = QLabel("lblDialog")
btnOK = QPushButton("OK")
layout.addWidget(btnOK, 5, 1)
def randomOptions():
rdmOpt = [2,3,4]
random.shuffle(rdmOpt)
optGreen = QRadioButton()
optYellow = QRadioButton()
optRed = QRadioButton()
lblGreen = QLabel("Green")
lblYellow = QLabel("Yellow")
lblRed = QLabel("Red")
layout.addWidget(lblWBS, 0, 1)
layout.addWidget(lblDialog, 1, 1)
layout.addWidget(optGreen, rdmOpt[0], 0)
layout.addWidget(lblGreen, rdmOpt[0], 1)
layout.addWidget(optYellow, rdmOpt[1], 0)
layout.addWidget(lblYellow, rdmOpt[1], 1)
layout.addWidget(optRed, rdmOpt[2], 0)
layout.addWidget(lblRed, rdmOpt[2], 1)
self.setLayout(layout)
randomOptions()
btnOK.clicked.connect(randomOptions)
self.setWindowTitle("PALCDMS")
app = QApplication(sys.argv)
dialog = GameDialog()
dialog.show()
app.exec_()
The way to implement your idea you have errors, since you are creating new objects.
You should Save a list of the widgets that will change position and remove them with removeWidget()
before making the changes, and then put them back in the layout.
class GameDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QGridLayout(self)
lblWBS = QLabel("lblWBS")
lblDialog = QLabel("lblDialog")
btnOK = QPushButton("OK")
layout.addWidget(btnOK, 5, 1)
optGreen = QRadioButton()
optYellow = QRadioButton()
optRed = QRadioButton()
lblGreen = QLabel("Green")
lblYellow = QLabel("Yellow")
lblRed = QLabel("Red")
layout.addWidget(lblWBS, 0, 1)
layout.addWidget(lblDialog, 1, 1)
l = [optGreen, lblGreen, optYellow, lblYellow, optRed, lblRed]
def randomOptions():
for w in l:
layout.removeWidget(w)
rdmOpt = [2,3,4]
random.shuffle(rdmOpt)
for i in range(len(l)):
layout.addWidget(l[i], rdmOpt[i//2], i % 2)
randomOptions()
btnOK.clicked.connect(randomOptions)
self.setWindowTitle("PALCDMS")
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.