尝试对变量进行插值时出错

时间:2019-06-02 09:28:31

标签: python python-3.x attributes kivy kivy-language

我似乎无法弄清楚什么“超级”对象没有属性“ getattr ”的含义。我不知道为什么会收到此错误。

这是一个简单的猜谜游戏,我正在使用Kivy作为GUI

import math
import kivy
kivy.require('1.11.0')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button

class Layout(GridLayout):

    def __init__(self, **kwargs):
        super(Layout, self).__init__(**kwargs)

        self.cols = 3
        self.guesses = 10
        self.hasWon = False
        self.pressedStart = False
        self.ids.start.text = 'Start'
        self.ids.gameText.text = 'Press Start when have chosen your number and are ready!'
        self.ids.instructions.text = 'Choose any number between 0 and 20000 and I will guess it in 10 tries!'
        self.max_guess = 20000
        self.min_guess = 0
        self.guess = math.ceil((self.max_guess - self.min_guess) / 2)
        self.ids.guessText.text = f'Guess left: {self.guesses}'


    def higher(self):
        if self.pressedStart == True and self.hasWon == False:
            self.guesses -= 1
            self.min_guess += self.guess
            self.ids.startText.text = f'Is your number {self.guess}?'
        else:
            pass
    File "guessing_game.py", line 41, in higher
      self.ids.startText.text = f'Is your number {self.guess}?'
    File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
    AttributeError: 'super' object has no attribute '__getattr__'

1 个答案:

答案 0 :(得分:0)

问题-AttributeError

  self.ids.startText.text = f'Is your number {self.guess}?'
File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'

根本原因

这意味着 ids.startText 字典类型的属性或对象self.ids中都不存在属性Layout