Search code examples
pythonmobilekivy

Simple calculator issue in Kivy


I'm working on a calculator which calculates depending on how much you are spending, how much money you'll have at the end of the month.

I'll provide you with both the .kv and .py. The thing is, I've done the visuals and I've created the calculator class which contains the math, now I need to combine it into a functioning GUI app.

So the thing I want to achieve is that the app, when you click on the button labeled "izracunaj", the Calculator class ( in ''' ) which should take the user input and calculate it and return a result.

main.py

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class Screen(GridLayout):
    def __init__(self, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self.cols = 1

        self.add_widget(Label(text="Koliko imas na racunu"))
        self.stanjeNovaca = TextInput(multiline=False)
        self.add_widget(self.stanjeNovaca)

        self.add_widget(Label(text="Koliko dana do place"))
        self.stanjeDani = TextInput(multiline=False)
        self.add_widget(self.stanjeDani)

        self.add_widget(Label(text="Koliko trosis na dan"))
        self.stanjePotrosnja = TextInput(multiline=False)
        self.add_widget(self.stanjePotrosnja)

'''
class Calculator():
    def calculation():
        stanjeNovaca = input("Koliko imas para na racunu? ")
        stanjeDani = input("Koliko dana do place? ")
        stanjePotrosnja = input("koliko trosis na dan? ")
        svakiDanTrosis = stanjeDani*stanjePotrosnja
        naKrajuMjeseca = stanjeNovaca-svakiDanTrosis
        print("Ako svaki dan trosis {}, na kraju mjeseca ce ti ostati {}").format(stanjePotrosnja, naKrajuMjeseca)
        return calculation();
'''
class CalculatorApp(App):
  def build(self):
    return Screen()


CalculatorApp().run()

kv

<Screen>
    Button:
        text: "izracunaj"

Solution

  • I fixed your program. The added command in the kv-file triggers the calculations when you press the button and the result will be printed.

    main.py

    import kivy
    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.image import Image
    from kivy.uix.label import Label
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.textinput import TextInput
    
    class Screen(GridLayout):
        def __init__(self, **kwargs):
            super(Screen, self).__init__(**kwargs)
            self.cols = 1
    
            self.add_widget(Label(text="Koliko imas na racunu"))
            self.stanjeNovaca = TextInput(multiline=False)
            self.add_widget(self.stanjeNovaca)
    
            self.add_widget(Label(text="Koliko dana do place"))
            self.stanjeDani = TextInput(multiline=False)
            self.add_widget(self.stanjeDani)
    
            self.add_widget(Label(text="Koliko trosis na dan"))
            self.stanjePotrosnja = TextInput(multiline=False)
            self.add_widget(self.stanjePotrosnja)
    
        def calculation(self):
            svakiDanTrosis = float(self.stanjeDani.text) * float(self.stanjePotrosnja.text)
            naKrajuMjeseca = float(self.stanjeNovaca.text) - svakiDanTrosis
            print("Ako svaki dan trosis {}, na kraju mjeseca ce ti ostati {}".format(self.stanjePotrosnja.text, naKrajuMjeseca))
            return naKrajuMjeseca
    
    class CalculatorApp(App):
      def build(self):
        return Screen()
    
    
    CalculatorApp().run()
    

    calculator.kv

    <Screen>
        Button:
            text: "izracunaj"
            on_release: root.calculation()
    

    If you want to show the result in the GUI, just add a Widget (e.g. a Label) to your Screen and set its text accordingly.