1

I'm working with PyQt6. I've set up a grid layout of a bunch of different labels called b0, b1...b6...d1... I have a letter dictionary that is edited whenever I input a word

self.b0 = QLabel(self) etc...
letter_dict = {'b':3,'c':3,'d':3,'f':3,'g':3,'h':3,'k':3,'l':3,'m':3,'n':3,'p':3,'r':3,'s':3,'t':3,'v':3,'w':3,'y':3}

for letter in letter_dict:
          letterposition = letter +str(letter_dict[letter])
          print(letterposition)

I want to then update the label called b0, or b1 etc. accordingly. I tried using self.letterposition.setText(letter) , as that should call self.b0.setText(letter). That doesn't seem to be working. I assume it's something to do with the self. instance interacting with a variable? This isn't the whole code, but everything else seems to be working.

2
  • "a bunch of different labels called b0, b1...b6...d1..." You should learn about lists. Commented Apr 8, 2022 at 22:17
  • @Yaya77 As already suggested in your other question, creating each instance like that is highly discouraged: it's ineffective, makes the code unnecessarily long, difficult to maintain and prone to errors and problems. Really, don't do this, it's just bad (if not terrible) practice, and if you keep asking questions that are obviously related to this aspect, nobody will answer you. Commented Apr 8, 2022 at 22:48

1 Answer 1

1

You should use a list:

class MyApp:
    def __init__():
        self.labels = [QLabel(self) for _ in range(7)]

Now you can do something like

self.labels[0].setText(letter)

But you don't have to use 0 explicitly. You can use a variable instead.

For a grid, you can create a list of lists. I'll leave the details as an exercise for the reader.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.