根据来自QComboBox的用户输入来添加和删除动态生成的QLineEdit小部件

时间:2019-02-17 22:43:51

标签: python python-3.x pyside2

我正在构建一个应用程序来“滚动”同一张面孔的多个骰子,因此,如果需要滚动5个8面骰子,可以在QComboBox中的标签D8下选择5,然后弹出5个QLineEdit小部件并显示值显示的5个“骰子”中。

当前,在选择QComboBox中的数字后,我能够显示正确数量的QLineEdit小部件。当我尝试完全删除模具胶卷显示器或将QLineEdit小部件的数量设置为小于第一个值时,就会出现问题。有时它可以正常工作,但永远不会在我尝试将显示设置为0时出现,否则我会抛出KeyError或RuntimeError。

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QGridLayout, QPushButton, QLineEdit, QLabel, QComboBox, QDialog, QApplication

class RollMultiDiePopup(QDialog):
    def __init__(self, parent=None):
        super(RollMultiDiePopup, self).__init__(parent)
        #self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Additional Dies to roll')
        self.setContentsMargins(5, 5, 5, 5)
        self.comboDict = {"D4": 4, "D6": 6, "D8": 8, "D10": 10, "D12": 12, "D20": 20}
        self.comboLblDict = {"# of D4s": 0, "# of D6s": 0, "# of D8s": 0, "# of D10s": 0, "# of D12s": 0, "# of D20s": 0}
        self.layoutGrid = QGridLayout(self)
        self.layoutGrid.setSpacing(10)
        self.gridRow = 0
        self.gridCol = 0
        self.comboLbl = {}
        self.comboBoxes = {}
        self.addnlInputs = {}
        self.generatecombolabels()
        self.generatecomboboxes()
        self.generaterollbuttons()
        self.setLayout(self.layoutGrid)
        self.adjustSize()

    def generatecombolabels(self):
        self.gridCol = 0
        for key, val in self.comboLblDict.items():
            self.gridCol = self.gridCol + 1
            self.comboLbl[key] = QLabel(key, self)
            self.layoutGrid.addWidget(self.comboLbl[key], 0, self.gridCol)

    def generatecomboboxes(self):
        self.gridCol = 0
        for key, val in self.comboDict.items():
            self.gridCol = self.gridCol + 1
            self.comboBoxes[key] = QComboBox(self)
            self.comboBoxes[key].addItems([str(x) for x in range(21)])
            #self.comboBoxes[key].activated.connect(self.adddisplays)
            self.layoutGrid.addWidget(self.comboBoxes[key], 1, self.gridCol)

    def generaterollbuttons(self):
        self.gridCol = 0
        for key, val in self.comboDict.items():
            self.gridCol = self.gridCol + 1
            buttons = QPushButton("Roll " + key + "s", self)
            buttons.setToolTip("Roll D" + str(self.comboDict[key]) + " (1 - " + str(self.comboDict[key]) + ")")
            #buttons.clicked.connect(partial(self.rolldie, val))
            buttons.clicked.connect(self.adddisplays)
            self.layoutGrid.addWidget(buttons, 2, self.gridCol)

    def rolldie(self):
        pass

    def adddisplays(self):
        d4s = int(self.comboBoxes["D4"].currentText())
        d6s = int(self.comboBoxes["D6"].currentText())
        d8s = int(self.comboBoxes["D8"].currentText())
        d10s = int(self.comboBoxes["D10"].currentText())
        d12s = int(self.comboBoxes["D12"].currentText())
        d20s = int(self.comboBoxes["D20"].currentText())
        dies = {1: d4s, 2: d6s, 3: d8s, 4: d10s, 5: d12s, 6: d20s}
        #if d4s == 0 or d6s == 0 or d8s == 0 or d10s == 0 or d12s == 0 or d20s == 0:
        self.removeaddeddisplays()
        if d4s > 0 or d6s > 0 or d8s > 0 or d10s > 0 or d12s > 0 or d20s > 0:
            for keys, vals in dies.items():
                self.gridRow = 3
                for i in range(vals):
                    self.gridRow += 1
                    self.addnlInputs["addnlInput" + str(i + 1)] = QLineEdit(self)
                    self.addnlInputs["addnlInput" + str(i + 1)].setAlignment(Qt.AlignRight)
                    self.addnlInputs["addnlInput" + str(i + 1)].setText("")
                    self.addnlInputs["addnlInput" + str(i + 1)].setPlaceholderText("Die Roll #" + str(i + 1))
                    self.layoutGrid.addWidget(self.addnlInputs["addnlInput" + str(i + 1)], self.gridRow, keys)

    def removeaddeddisplays(self):
        try:
            for i in range(3, 21):
                self.layoutGrid.removeWidget(self.addnlInputs["addnlInput" + str(i + 1)])
                self.addnlInputs["addnlInput" + str(i + 1)].deleteLater()
                self.addnlInputs["addnlInput" + str(i + 1)] = None
                self.adjustSize()
        except KeyError:
            print("1")
        except RuntimeError:
            print("2")


if __name__ == "__main__":
    app = QApplication()
    w = RollMultiDiePopup()
    w.show()
    app.exec_()

这是用于初始生成QComboBoxes并填充#s列表的代码。

self.gridCol = 0
for key, val in self.comboDict.items():
    self.gridCol = self.gridCol + 1
    self.comboBoxes[key] = QComboBox(self)
    self.comboBoxes[key].addItems([str(x) for x in range(21)])
    #self.comboBoxes[key].activated.connect(self.adddisplays)
    self.layoutGrid.addWidget(self.comboBoxes[key], 1, self.gridCol)

这是当与D#标签/组合框相对应的按钮时添加显示的代码

d4s = int(self.comboBoxes["D4"].currentText())
d6s = int(self.comboBoxes["D6"].currentText())
d8s = int(self.comboBoxes["D8"].currentText())
d10s = int(self.comboBoxes["D10"].currentText())
d12s = int(self.comboBoxes["D12"].currentText())
d20s = int(self.comboBoxes["D20"].currentText())
dies = {1: d4s, 2: d6s, 3: d8s, 4: d10s, 5: d12s, 6: d20s}
#if d4s == 0 or d6s == 0 or d8s == 0 or d10s == 0 or d12s == 0 or d20s == 0:
self.removeaddeddisplays()
if d4s > 0 or d6s > 0 or d8s > 0 or d10s > 0 or d12s > 0 or d20s > 0:
    for keys, vals in dies.items():
        self.gridRow = 3
        for i in range(vals):
            self.gridRow += 1
            self.addnlInputs["addnlInput" + str(i + 1)] = QLineEdit(self)
            self.addnlInputs["addnlInput" + str(i + 1)].setAlignment(Qt.AlignRight)
            self.addnlInputs["addnlInput" + str(i + 1)].setText("")
            self.addnlInputs["addnlInput" + str(i + 1)].setPlaceholderText("Die Roll #" + str(i + 1))
            self.layoutGrid.addWidget(self.addnlInputs["addnlInput" + str(i + 1)], self.gridRow, keys)

这是removeaddeddisplays功能

try:
    for i in range(3, 21):
        self.layoutGrid.removeWidget(self.addnlInputs["addnlInput" + str(i + 1)])
        self.addnlInputs["addnlInput" + str(i + 1)].deleteLater()
        self.addnlInputs["addnlInput" + str(i + 1)] = None
        self.adjustSize()
except KeyError:
     print("1")
except RuntimeError:
     print("2")

我尝试了几种不同的方式来删除窗口小部件,并且最终得到不同的结果,仅删除了所有其他显示,所有内容,每三分之一..etc。这是迄今为止我发现的最一致的方法。

我使用的是python 3和pyside 2,我会在显示器正常工作后担心处理模版。

1 个答案:

答案 0 :(得分:2)

如果要从布局中删除窗口小部件,只需使用deleteLater,但这并不意味着如果将引用存储在列表中,则这些引用将被删除。在我的策略中,我使用deleteLater消除了所有QLineEdits,重置了存储它的列表,并创建了新的QLineEdits。

from PySide2 import QtCore, QtWidgets

class RollMultiDiePopup(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(RollMultiDiePopup, self).__init__(parent)

        self._data = {
            "D4": 4, 
            "D6": 6, 
            "D8": 8, 
            "D10": 10, 
            "D12": 12, 
            "D20": 20
        }

        self._lineedits = [[] for _ in self._data]
        self._comboboxes = []

        grid_layout = QtWidgets.QGridLayout(self)
        self.create_labels()
        self.create_comboboxes()
        self.create_buttons()

    def create_labels(self):
        row = 0
        grid_layout = self.layout()
        for i, k in enumerate(self._data.keys()):
            label = QtWidgets.QLabel("# of {}s".format(k))
            grid_layout.addWidget(label, row, i)

    def create_comboboxes(self):
        row = 1
        grid_layout = self.layout()
        for i in range(len(self._data)):
            combo = QtWidgets.QComboBox()
            combo.addItems([str(j) for j in range(21)])
            self._comboboxes.append(combo)
            grid_layout.addWidget(combo, row, i)

    def create_buttons(self):
        row = 2
        grid_layout = self.layout()
        for i, (k, v) in enumerate(self._data.items()):
            button = QtWidgets.QPushButton("Roll {}s".format(k))
            button.setToolTip("Roll {}(1 - {})".format(k, v))
            button.clicked.connect(self.update_lineedits)
            grid_layout.addWidget(button, row, i)

    @QtCore.Slot()
    def update_lineedits(self):
        row = 3
        grid_layout = self.layout()
        for r in self._lineedits:
            for le in r:
                le.deleteLater()
        self._lineedits = [[] for _ in self._data]
        for i, (les,combo) in enumerate(zip(self._lineedits, self._comboboxes)):
            v = int(combo.currentText())
            for j in range(v):
                le = QtWidgets.QLineEdit()
                le.setPlaceholderText("Die Roll #{}".format(j+1))
                grid_layout.addWidget(le, row+j, i)
                les.append(le)
        QtCore.QTimer.singleShot(0, self.adjust_size)

    @QtCore.Slot()
    def adjust_size(self):
        animation = QtCore.QPropertyAnimation(self, b"size", self)
        animation.setStartValue(self.size())
        animation.setEndValue(self.minimumSizeHint())
        animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = RollMultiDiePopup()
    w.show()
    sys.exit(app.exec_())