0

I am a Python / Kivy beginner, I would like to get a sum of the values ​​a and b in the label "outputWindow", can someone help me? Thanks!

class Example(App):

    def build(self):
        layout = BoxLayout(orientation='vertical')

        self.otputWindow = Label(text="...")

        self.aClick = Button(text="Calc >>")

        self.aClick.bind(on_press=self.first_number)
        self.aClick.bind(on_press=self.second_number)


        layout.add_widget(self.otputWindow)
        layout.add_widget(self.aClick)


        return layout



    def first_number(self, *args):
        a = 5

    def second_number(self, *args):
        b = 10


if __name__ == '__main__':
    Example().run()
AS Mackay
  • 2,831
  • 9
  • 19
  • 25

1 Answers1

1

I haven't used Kivy myself, I have one comment on the python bit:

The variables a and b you are using in your methods first_number() and second_number() are only defined in the scope of those methods. You could either assign self.a = 5 or you can have the methods return the number instead. By just writing a = 5 you won't be able to refer to a anywhere else than in your method.

Here is one suggestion for your code (although it is probably incomplete because I don't know exactly what you are trying to do):

class Example(App):

    def build(self):
        layout = BoxLayout(orientation='vertical')

        self.first_number()
        self.second_number()
        self.otputWindow = Label(text=str(self.a + self.b))

        self.aClick = Button(text="Calc >>")

        self.aClick.bind(on_press=self.first_number)
        self.aClick.bind(on_press=self.second_number)


        layout.add_widget(self.otputWindow)
        layout.add_widget(self.aClick)

        return layout

    def first_number(self, *args):
        self.a = 5

    def second_number(self, *args):
        self.b = 10

if __name__ == '__main__':
    Example().run()