Making PushButton widgets for a program. The intent was to create each PushButton, connect it to a function which compared two string values, ap.parse_answer()
, then add the PushButton to the appropriate cell of a QGridLayout:
answers = ["this", "that", "the other", "one more"]
correct_answer = "this"
for grid_pos in [(i,j) for i in range(0,2) for j in range(0,2)]:
answer_disp = AnswerDisplay()
current_answer = answers.pop()
answer_disp.setText(current_answer)
answer_disp.clicked.connect(
lambda: self.ap.parse_answer(current_answer, answer))
answer_grid.addWidget(answer_disp, *grid_pos)
Here is the AnswerDisplay class:
class AnswerDisplay(QtGui.QPushButton):
def __init__(self):
super(AnswerDisplay, self).__init__()
answer_font = QtGui.QFont()
answer_font.setWeight(24)
answer_font.setPixelSize(20)
self.setFont(answer_font)
Unfortunately, what happens is the same function gets connected to each button. The last function generated end up on all the buttons, so it seems the connect is being reapplied to previous created buttons. But how do I address this? My approach can't be completely invalid because the setText() function correctly sets the text for each button without overwriting the previous assignments.
I tried to address the issue making an single AnswerDisplay and then copying it with deepcopy():
for grid_pos in [(i,j) for i in range(0,2) for j in range(0,2)]:
disp = AnswerDisplay()
answer_disp = deepcopy(disp)
super(AnswerDisplay, answer_disp).__init__()
...
but it produced the same undesired result.
I've done some searching, but all I've found are questions from people trying to get the kind of result I'm trying not to get. Any help would be appreciated.