【发布时间】:2017-11-28 01:06:43
【问题描述】:
我目前正在努力通过按下按钮来更新一堆标签,我可以通过Kivy: How to refernce kv ID in Python?得到答案
但是,现在我在我的实际应用程序中,我需要能够获取 用户输入 值并更新现有标签的功能。我稍微修改了这个示例,我只是将用户输入作为起始数字,每次单击按钮时添加一个,并在所有这些的右侧显示运行总计。
我现在拥有的代码如下所示,但加法 1 似乎没有被处理(它只是在单击后保持相同的总数)。任何想法为什么?非常感谢!
这是单击按钮之前的显示(运行总计只是用户输入):
我的 Python 文件:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
import random
class TestingWidget(BoxLayout):
# This is the kv id of the Label I would like to update
starting_number = StringProperty('Put your Starting Number here')
running_total = StringProperty(str(0))
#default text set
# This is the action I would like to happen when the button is pressed
def button_pressed(self):
self.running_total = str(int(self.running_total) + 1)
class TestButtonApp(App):
def build(self):
return TestingWidget()
if __name__ == '__main__':
TestButtonApp().run()
我的kv文件:
<TestingWidget>:
BoxLayout:
orientation: 'horizontal'
TextInput:
id: starting_number
hint_text: root.starting_number
Button:
id: add_one_button
text: 'Add 1 to Starting Number'
on_press: root.button_pressed()
Label:
id: running_total
text: starting_number.text
【问题讨论】: