I have searched through the "Similar Questions" tab, to no avail. I was wondering if this is possible. My goal is to iterate through each ComboBox and put a '1' or '0' into a temporary list depending if YES or NO is answered. My main goal is to get the YES/NO as binary data, so it can be converted to hexadecimal and dumped to a file.
I have seventy-two QComboBoxes, named comboBox_25 through comboBox_96
@QtCore.pyqtSlot()
def on_actionSave_triggered(self):
answers = []
i = 25
while i < 97:
if self.comboBox_[i].currentText() == 'Yes' and self.comboBox_[i].currentText() != '':
answers += '1'
i += 1
elif self.comboBox_[i].currentText() == 'No' and self.comboBox_[i].currentText() != '':
answers += '0'
i += 1
print(answers)
Debug Output:
class 'AttributeError'>, AttributeError("'writeWindow' object has no attribute 'comboBox_'"), < traceback object at 0x000001F0783E0A00 > )
I have also tried this:
def on_actionSave_triggered(self):
answers = []
box_list = ['comboBox_25', 'comboBox_28','comboBox_27']
for i in range (len(box_list)):
if self.box_list[i].currentText() == 'Yes' and self.box_list[i].currentText() != '':
answers += '1'
elif self.box_list[i].currentText() == 'No' and self.box_list[i].currentText() != '':
answers += '0'
print(answers)
Debug Output:
class 'AttributeError'>, AttributeError("'writeWindow' object has no attribute 'box_list'"), < traceback object at 0x0000025F9DD00DC0 > )
This is how I was doing it before, my program works and executes, but I am trying to optimize my program.
answer = []
binary = ''
answer.append(self.answerOne.currentText())
answer.append(self.answerTwo.currentText())
answer.append(self.answerThree.currentText())
answer.append(self.answerFour.currentText())
answer.append(self.answerFive.currentText())
answer.append(self.answerSix.currentText())
answer.append(self.answerSeven.currentText())
answer.append(self.answerEight.currentText())
answer.append(self.answerNine.currentText())
answer.append(self.answerTen.currentText())
answer.append(self.answerEleven.currentText())
answer.append(self.answerTwelve.currentText())
answer.append(self.answerThirteen.currentText())
answer.append(self.answerFourteen.currentText())
answer.append(self.answerFifteen.currentText())
answer.append(self.answerSixteen.currentText())
answer.append(self.answerSeventeen.currentText())
answer.append(self.answerEightteen.currentText())
answer.append(self.answerNineteen.currentText())
answer.append(self.answerTwenty.currentText())
answer.append(self.answerTwentyone.currentText())
answer.append(self.answerTwentytwo.currentText())
answer.append(self.answerTwentythree.currentText())
answer.append(self.answerTwentyfour.currentText())
answer.append(self.answerTwentyfive.currentText())
answer.append(self.answerTwentysix.currentText())
answer.append(self.answerTwentyseven.currentText())
answer.append(self.answerTwentyeight.currentText())
answer.append(self.answerTwentynine.currentText())
answer.append(self.answerThirty.currentText())
answer.append(self.answerThirtyone.currentText())
answer.append(self.answerThirtytwo.currentText())
answer.append(self.answerThirtythree.currentText())
answer.append(self.answerThirtyfour.currentText())
answer.append(self.answerThirtyfive.currentText())
answer.append(self.answerThirtysix.currentText())
answer.append(self.answerThirtyseven.currentText())
answer.append(self.answerThirtyeight.currentText())
answer.append(self.answerThirtynine.currentText())
answer.append(self.answerFourty.currentText())
answer.append(self.answerFourtyone.currentText())
answer.append(self.answerFourtytwo.currentText())
answer.append(self.answerFourtythree.currentText())
answer.append(self.answerFourtyfour.currentText())
answer.append(self.answerFourtyfive.currentText())
answer.append(self.answerFourtysix.currentText())
answer.append(self.answerFourtyseven.currentText())
answer.append(self.answerFourtyeight.currentText())
answer.append(self.answerFourtynine.currentText())
answer.append(self.answerFifty.currentText())
answer.append(self.answerFiftyone.currentText())
answer.append(self.answerFiftytwo.currentText())
answer.append(self.answerFiftythree.currentText())
answer.append(self.answerFiftyfour.currentText())
answer.append(self.answerFiftyfive.currentText())
answer.append(self.answerFiftysix.currentText())
answer.append(self.answerFiftyseven.currentText())
answer.append(self.answerFiftyeight.currentText())
answer.append(self.answerFiftynine.currentText())
answer.append(self.answerSixty.currentText())
answer.append(self.answerSixtyone.currentText())
answer.append(self.answerSixtytwo.currentText())
answer.append(self.answerSixtythree.currentText())
answer.append(self.answerSixtyfour.currentText())
answer.append(self.answerSixtyfive.currentText())
answer.append(self.answerSixtysix.currentText())
answer.append(self.answerSixtyseven.currentText())
answer.append(self.answerSixtyeight.currentText())
answer.append(self.answerSixtynine.currentText())
answer.append(self.answerSeventy.currentText())
answer.append(self.answerSeventyone.currentText())
answer.append(self.answerSeventytwo.currentText())
for i in range(72):
if answer[i] == '':
msg2 = QMessageBox()
msg2.setWindowTitle("Brennan's Auto OPC")
msg2.setWindowIcon((QtGui.QIcon('B.ico')))
msg2.setText("Ensure all of the selections have a YES/NO, otherwise this will return")
x = msg2.exec_()
return
elif answer[i] == 'Yes':
binary += '1'
elif answer[i] == 'No':
binary += '0'
EDIT: Here is what I ended up doing..
def on_actionSave_triggered(self):
binary = ''
comboBoxes = \
[self.comboBox_25.currentText(), self.comboBox_26.currentText(), self.comboBox_27.currentText(),
self.comboBox_28.currentText(), self.comboBox_29.currentText(), self.comboBox_30.currentText(),
self.comboBox_31.currentText(), self.comboBox_32.currentText(), self.comboBox_33.currentText(),
self.comboBox_34.currentText(), self.comboBox_35.currentText(), self.comboBox_36.currentText(),
self.comboBox_37.currentText(), self.comboBox_38.currentText(), self.comboBox_39.currentText(),
self.comboBox_40.currentText(), self.comboBox_41.currentText(), self.comboBox_42.currentText(),
self.comboBox_43.currentText(), self.comboBox_44.currentText(), self.comboBox_45.currentText(),
self.comboBox_46.currentText(), self.comboBox_47.currentText(), self.comboBox_48.currentText(),
self.comboBox_49.currentText(), self.comboBox_50.currentText(), self.comboBox_51.currentText(),
self.comboBox_52.currentText(), self.comboBox_53.currentText(), self.comboBox_54.currentText(),
self.comboBox_55.currentText(), self.comboBox_56.currentText(), self.comboBox_57.currentText(),
self.comboBox_58.currentText(), self.comboBox_59.currentText(), self.comboBox_60.currentText(),
self.comboBox_61.currentText(), self.comboBox_62.currentText(), self.comboBox_63.currentText(),
self.comboBox_64.currentText(), self.comboBox_65.currentText(), self.comboBox_66.currentText(),
self.comboBox_67.currentText(), self.comboBox_68.currentText(), self.comboBox_69.currentText(),
self.comboBox_70.currentText(), self.comboBox_71.currentText(), self.comboBox_72.currentText(),
self.comboBox_73.currentText(), self.comboBox_74.currentText(), self.comboBox_75.currentText(),
self.comboBox_76.currentText(), self.comboBox_77.currentText(), self.comboBox_78.currentText(),
self.comboBox_79.currentText(), self.comboBox_80.currentText(), self.comboBox_81.currentText(),
self.comboBox_82.currentText(), self.comboBox_83.currentText(), self.comboBox_84.currentText(),
self.comboBox_85.currentText(), self.comboBox_86.currentText(), self.comboBox_87.currentText(),
self.comboBox_88.currentText(), self.comboBox_89.currentText(), self.comboBox_90.currentText(),
self.comboBox_91.currentText(), self.comboBox_92.currentText(), self.comboBox_93.currentText(),
self.comboBox_94.currentText(), self.comboBox_95.currentText(), self.comboBox_96.currentText()]
for combos in range(len(comboBoxes)):
value = comboBoxes[combos]
command = ("self.comboBox_" + str(combos + 25))
command = command + ".setStyleSheet("'"background-color: yellow "'")"
commandClear = ("self.comboBox_" + str(combos + 25))
commandClear = commandClear + ".setStyleSheet("'"background-color: rgb(195, 195, 195)"'")"
if value == '':
if include.displayedWarning == 0:
fileMessage = QMessageBox()
fileMessage.setWindowTitle("AutoOPC")
fileMessage.setWindowIcon((QtGui.QIcon('main.ico')))
fileMessage.setText("Please ensure all fields have an answer! "
"Focus your attention to highlighted field(s)")
sendMessage = fileMessage.exec_()
include.displayedWarning += 1
exec(command)
elif value == 'Yes':
exec(commandClear)
binary += '1'
else:
exec(commandClear)
binary += '0'