简体   繁体   English

Kivy计算器应用

[英]Kivy calculator application

Trying to build a simple calculator with the kivy framework. 尝试使用kivy框架构建一个简单的计算器。

This is in my main.py file 这是在我的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()

and in my kivy file instead of doing: 并在我的kivy文件中而不是这样做:

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

i want to use the show function that i defined in my main.py 我想使用我在main.py中定义的show函数

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

but i get an AttributeError: Calculator object has no attribute 'text' 但是我得到了AttributeError:计算器对象没有属性'text'

You have this line in the show method of Calculator : 您在Calculator的show方法中具有以下内容:

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

This refers to self.text, but the code you've given never sets this attribute for the Calculator, therefore you get the given error: Calculator object has no attribute 'text' 这是指self.text,但是您提供的代码永远不会为Calculator设置此属性,因此会出现给定的错误: Calculator object has no attribute 'text'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM