【问题标题】:Kivy: Variable in a class?Kivy:班级中的变量?
【发布时间】: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

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    问题在于Label 与TextInput 绑定,如果您在更改Label 时更改它,TextInput 会再次更新它,给人感觉它没有改变。您应该做的是使用 on_text 事件更改文本,如下所示:

    *.py

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.widget import Widget
    from kivy.properties import StringProperty
    
    class TestingWidget(BoxLayout):
        starting_number = StringProperty("")
        running_total = StringProperty(str(0))
        def button_pressed(self):
            if self.running_total != "":
                self.running_total = str(int(self.running_total) + 1)
    
        def text_changed(self, instance, value):
            self.running_total = value
    
    class TestButtonApp(App):
        def build(self):
            return TestingWidget()
    
    if __name__ == '__main__':
        TestButtonApp().run()
    

    *.kv

    <TestingWidget>:
        BoxLayout:
            orientation: 'horizontal'
            TextInput:
                id: starting_number
                hint_text: "Put your Starting Number here"
                on_text: root.text_changed(*args)
                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: root.running_total
    

    【讨论】:

    • 明白!谢谢!
    【解决方案2】:

    解决方案是只更改您的 kv 文件,而不需要更改您的 Python 脚本。变化如下:

    kv 文件

    1。 TextInput:添加 on_text 事件

    文本输入的文本存储在其 TextInput.text 属性中。要在文本更改时运行回调,请执行以下操作。

            on_text:
                root.running_total = self.text
    

    2。标签:引用StringProperty,root.running_total

    替换:

            text: starting_number.text
    

    与:

            text: root.running_total
    

    testbutton.kv

    #:kivy 1.10.0
    
    <TestingWidget>:
        BoxLayout:
            orientation: 'horizontal'
            TextInput:
                id: starting_number
                hint_text: root.starting_number
                on_text:
                    root.running_total = self.text
            Button:
                id: add_one_button
                text: 'Add 1 to Starting Number'
                on_press: root.button_pressed()
            Label:
                id: running_total
                text: root.running_total
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      相关资源
      最近更新 更多