我正在构建一个应用程序来“滚动”同一张面孔的多个骰子,因此,如果我需要滚动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,200)
self.setWindowTitle('Additional Dies to roll')
self.setContentsMargins(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],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_()
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],self.gridCol)
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,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)],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,在显示器正常工作后,我会担心处理模版.