【发布时间】:2021-10-26 01:46:01
【问题描述】:
请帮助我,我的竞争即将来临
我尝试在 Kivy 中使用 OOP。这是我用于测试的简单 Python 代码:
class location:
def __init__(self, total_house, total_land):
self.total_house = total_house
self.total_land = total_land
class test(BoxLayout):
def addNum(self):
App.get_running_app().x.total_house += 1
class testApp(App):
x = location(NumericProperty(10),NumericProperty(5))
testApp().run()
这是我的 kv 文件:
<test>:
orientation: 'vertical'
Label:
text: str(app.x.total_house)
Button:
text: 'add'
on_press: root.addNum()
我希望输出为 10,当按下按钮时,数字加一。
请帮帮我,我是 KIVY 的新手
【问题讨论】:
-
你试过用
x = location(10, 5)替换x = location(NumericProperty(10),NumericProperty(5))吗? -
numberx是什么? -
@RufusVS 抱歉,实际上是 x.total_house
-
@Weebify 如果我用位置 (10,5) 替换它,我无法将它们添加 1。即使我按下按钮,它也会显示 10 和 5。 (对不起我的英语不好)
-
你检查我的答案了吗?
标签: python python-3.x kivy kivy-language kivymd