I am new to kivy and I'm trying to implement a shopping cart into my app. I have a variable named cart
and it is set to 0. I want to add to the variable when I press a button but when I do so, I get this error AssertionError: 7 is not callable
. Why am I getting this error exactly and how could I fix this?
I've tried a few things, like adding the global
command to cart
and putting the variable inside the FirstScreen
class but they both give the same error. I'm sure it's something simple that I'm missing out but any help would be appriciated!
Here an example code to recreate the problem.
.py file
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.popup import Popup
cart = 0
class FirstScreen(Screen):
def sizeSelection(self):
sizepopup = FloatLayout()
sizepop = Popup(title="Format", separator_color=[0.6, 0, 0, 1], content=sizepopup,title_font=("Gothic"), size_hint=(0.6, 0.6))
sizepopup.add_widget(Label(text="Choose a format", font_name="Gothic", pos_hint={"x": 0, "y": 0.4}))
sizepopup.add_widget(Button(text="Small", font_name="Gothic", size_hint=(1, 0.15), pos_hint={"x": 0, "y": 0.6}, on_release = cart + 7 ))
sizepopup.add_widget(Button(text="Back", font_name="Gothic", size_hint=(0.8, 0.15), pos_hint={"x": 0.10, "y": 0}, on_release=sizepop.dismiss))
sizepop.open()
class WindowManager(ScreenManager):
pass
class exampleApp(App):
def build(self):
return WindowManager()
if __name__ == "__main__":
exampleApp().run()
.kv file
<WindowManager>:
FirstScreen:
<FirstScreen>:
FloatLayout:
Button:
text: "Add to cart"
size_hint: 0.5,0.1
pos_hint: {"x": 0.25,"y":0.5}
on_release:
root.sizeSelection()
You can accomplish that by setting your on_release
to a method that increments cart
:
sizepopup.add_widget(Button(text="Small", font_name="Gothic", size_hint=(1, 0.15), pos_hint={"x": 0, "y": 0.6}, on_release=self.increment_cart))
Then, add that method to the FirstScreen
class:
def increment_cart(self, button):
global cart
cart += 7
print('cart =', cart)