在python pyqt6中使用变量调用类中先前定义的对象
我正在使用 PyQt6。我已经设置了一堆不同标签的网格布局,称为 b0、b1...b6...d1... 我有一个字母字典,每当我输入一个
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)
我想要的单词时都会对其进行编辑,然后更新名为的标签相应地 b0 或 b1 等。 我尝试使用 self.letterposition.setText(letter) ,因为应该调用 self.b0.setText(letter) 。这似乎不起作用。我认为这与自我有关。实例与变量交互? 这不是完整的代码,但其他所有内容似乎都有效。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用列表:
现在您可以做类似的事情
,但是您不必明确使用
0
。您可以使用变量。对于网格,您可以创建列表的列表。我将细节作为读者的练习。
You should use a list:
Now you can do something like
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.