如何在 Kivy 中通过按下按钮更改变量的值
How to change value of a variable with a button press in Kivy
我是 kivy 的新手,我正在尝试在我的应用程序中实现购物车。我有一个名为 cart
的变量,它被设置为 0。我想在按下按钮时添加到该变量,但是当我这样做时,我得到了这个错误 AssertionError: 7 is not callable
。为什么我会收到这个错误,我该如何解决?
我尝试了一些方法,比如将 global
命令添加到 cart
并将变量放在 FirstScreen
class 中,但它们都给出相同的结果错误。我确定我错过了一些简单的事情,但我们将不胜感激!
这里是重现问题的示例代码。
.py 文件
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 文件
<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()
您可以通过将 on_release
设置为递增 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))
然后,将该方法添加到 FirstScreen
class:
def increment_cart(self, button):
global cart
cart += 7
print('cart =', cart)
我是 kivy 的新手,我正在尝试在我的应用程序中实现购物车。我有一个名为 cart
的变量,它被设置为 0。我想在按下按钮时添加到该变量,但是当我这样做时,我得到了这个错误 AssertionError: 7 is not callable
。为什么我会收到这个错误,我该如何解决?
我尝试了一些方法,比如将 global
命令添加到 cart
并将变量放在 FirstScreen
class 中,但它们都给出相同的结果错误。我确定我错过了一些简单的事情,但我们将不胜感激!
这里是重现问题的示例代码。
.py 文件
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 文件
<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()
您可以通过将 on_release
设置为递增 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))
然后,将该方法添加到 FirstScreen
class:
def increment_cart(self, button):
global cart
cart += 7
print('cart =', cart)