0

Sorry for an incoherent title, I don't know how to phrase this.

I'm using a for-loop to generate a bunch of alphabetical buttons in PyQt5. It looks something like this:

for i in range(0, 3): #rows
    for j in range(0, 9): #columns
        if i == 2 and j == 8: #27th iteration
           break

        letter = chr(letterStart)  #letterStart = 65, capital "A"
        if letterStart < 90:
           letterStart += 1 

        buttons[letter] = [QPushButton(letter), letterStart]
        buttons[letter][0].setStyleSheet(StyleSheets.buttonTypeSmall)
        buttons[letter][0].clicked.connect(lambda: print(chr(letterButtonsDict[letter][1]))) #The part I'm having issues with
        
        grid.addWidget(letterButtonsDict[letter][0], i, j)

If I print buttons it will print excatly what I want, a dictionary with all my buttons as well as what letter they represent. However, clicking them will not print their respective letter, only the last letter of the loop (Z). I have tried several different methods but without success.

How do I make it so that my "A-button" prints an A, and my "Z-button" prints a Z?

Remfy
  • 61
  • 4
  • 1
    See https://stackoverflow.com/q/4578861 and https://stackoverflow.com/q/35819538. Change to `lambda _, letter=letter: print(chr(letterButtonsDict[letter][1]))`. – musicamante Feb 18 '22 at 13:34
  • 1
    Also, instead of setting the same stylesheet for each widget, you can set on the parent (or the application), as long as it's properly written by using [selectors](https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types). – musicamante Feb 18 '22 at 13:36

0 Answers0