在python pyqt6中使用变量调用类中先前定义的对象

发布于 2025-01-20 04:24:51 字数 540 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2025-01-27 04:24:51

您应该使用列表:

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

现在您可以做类似的事情

self.labels[0].setText(letter)

,但是您不必明确使用0。您可以使用变量。

对于网格,您可以创建列表的列表。我将细节作为读者的练习。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文