为什么我的代码在Python中将布尔值分配给实例self?

huangapple go评论82阅读模式
英文:

why does my code assign boolean to instance self in python

问题

我在使用 getattr() 时遇到了问题,我不知道为什么我的代码将布尔值赋给了 self 实例,并且返回错误:AttributeError: 'bool' object has no attribute 'key'

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QLabel

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.ui()

    key = 0
    btns = ["Next", "Back"]

    def ui(self):
        for a in range(0, 2):
            btn = QPushButton(self.btns[a], self)
            btn.move(20, 20 * a + 10)
            btn.clicked.connect(getattr(MyApp, self.btns[a]))

#//////////////////////////////// 只是界面 //////////////////////////////////

        self.setWindowTitle('Question')
        self.setGeometry(100, 100, 100, 100)

        self.label1 = QLabel('0', self)
        self.label1.move(20, 60)

        self.show()

#////////////////////////////////////////////////////////////////////////////////

    def Next(self):
        if self.key < 5:
            self.key += 1
        self.label1.setText(str(self.key))

    def Back(self):
        if 1 <= self.key:
            self.key -= 1
        self.label1.setText(str(self.key))

#////////////////////////////////////////////////////////////////////////////////

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

追溯(最近一次调用):
文件 "C:\project\pyeditor\QnA.py",第 31 行,在 Next 中
if self.key < 5:
AttributeError: 'bool' object has no attribute 'key'

我使用了大量按钮,但我只拿了两个按钮作为示例,以使您更容易理解问题。它生成为一个重复的语句。我也知道,如果我逐个编写按钮函数,我就不必担心实例值的数据类型。但这样一来,事实上的目的就被掩盖了。我会感激您的帮助。

英文:

I'm using a dynamic function as getattr()
But I don't why my code assign boolean value to self instance and its return follows: AttributeError: 'bool' object has no attribute 'key'

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QLabel

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.ui()

    key = 0
    btns = [&quot;Next&quot;, &quot;Back&quot;]

    def ui(self):
        for a in range(0, 2):
            btn = QPushButton(self.btns[a], self)
            btn.move(20, 20 * a + 10)
            btn.clicked.connect(getattr(MyApp, self.btns[a]))

#//////////////////////////////// it&#39;s only UI //////////////////////////////////

        self.setWindowTitle(&#39;Question&#39;)
        self.setGeometry(100, 100, 100, 100)

        self.label1 = QLabel(&#39;0&#39;, self)
        self.label1.move(20, 60)

        self.show()

#////////////////////////////////////////////////////////////////////////////////

    def Next(self):
        if self.key &lt; 5:
            self.key += 1
        self.label1.setText(self.key)

    def Back(self):
        if 1 &lt;= self.key:
            self.key -= 1
        self.label1.setText(self.key)

#////////////////////////////////////////////////////////////////////////////////

if __name__ == &#39;__main__&#39;:
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

> Traceback (most recent call last):
File "C:\project\pyeditor\QnA.py", line 31, in Next
if self.key < 5:
AttributeError: 'bool' object has no attribute 'key'

I use a fairly large number of buttons, but I have only taken two buttons as an example to make it easier for you to understand the question. It is generated as a repetitive statement. I also know that if I write button functions one by one, I don't have to worry about the data type of instance values. But then the purpose of factoring is overshadowed. I'd appreciate your help.

答案1

得分: 1

The line btn.clicked.connect(getattr(MyApp, self.btns[a])) is the issue.
Here you are connecting to the functions MyApp.Next and MyApp.Back instead of the function self.Next and self.Back. The difference is that the class MyApp does not know self since only an instance of MyApp can access it.

How to fix

Replace btn.clicked.connect(getattr(MyApp, self.btns[a]))

with btn.clicked.connect(getattr(self, self.btns[a])).

Edit

You also have a small error with setText where you need to convert self.key to a string.

英文:

The line btn.clicked.connect(getattr(MyApp, self.btns[a])) is the issue.
Here you are connecting to the functions MyApp.Next and MyApp.Back instead of the function self.Next and self.Back. The difference is that the class MyApp does not know self since only an instance of MyApp can access it.

How to fix

Replace btn.clicked.connect(getattr(MyApp, self.btns[a]))

with btn.clicked.connect(getattr(self, self.btns[a])).

Edit

You also have a small error with setText where you need to convert self.key to a string.

huangapple
  • 本文由 发表于 2023年7月6日 20:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定