【问题标题】:Kivy calculator applicationKivy 计算器应用程序
【发布时间】:2014-07-29 03:54:34
【问题描述】:

尝试用 kivy 框架构建一个简单的计算器。

这是在我的 main.py 文件中

class Calculator(AnchorLayout):

inputs = ObjectProperty(None)

def backward(self, express):
    if express:
        self.display.text = express[:-1]

def show(self):
    self.inputs.text = self.inputs.text + self.text

def calculate(self, express):
    if not express: return

    try:
        self.display.text = str( eval(express) )
    except Exception:
        self.display.text = 'error'


class CalculatorApp(App):
    def build(self):
        return Calculator()


CalculatorApp().run()

在我的 kivy 文件中而不是这样做:

Button:
            text: '9'
            on_press: input_string.text += self.text

我想使用我在 main.py 中定义的 show 函数

Button:    
            text: '7'
            on_press: root.show()           

但我得到一个 AttributeError: Calculator object has no attribute 'text'

【问题讨论】:

    标签: python macos python-2.7 frameworks kivy


    【解决方案1】:

    Calculator的show方法中有这一行:

    self.inputs.text = self.inputs.text + self.text
    

    这是指 self.text,但您提供的代码从未为计算器设置此属性,因此您会收到给定的错误:Calculator object has no attribute 'text'

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多