user6142489
user6142489

Reputation: 532

Kivy: Variable in a class?

I'm currently working to update a bunch of labels with a button press, which I was able to get an answer with through Kivy: How to refernce kv ID in Python?

However, now that I'm in my actual app, I need the functionality to be able to take a user input value and update existing labels. I've modified the example slightly where I'm just taking a user input for a starting number, adding one with each button click and displaying the running total to the right of all of this.

The code I have now is shown below, but the addition of 1 doesn't seem to be processing (it's just staying with the same total after a click). Any ideas why? Thanks very much!

This is the display before the button is clicked (running total is just the user input):

enter image description here

My Python file:

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()

My kv file:

<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

Upvotes: 0

Views: 914

Answers (2)

ikolim
ikolim

Reputation: 16031

The solution is to only change your kv File and no changes required to your Python Script. The changes are as follow:

kv File

1. TextInput: Add on_text Event

The textinput’s text is stored in its TextInput.text property. To run a callback when the text changes, do the following.

        on_text:
            root.running_total = self.text

2. Label: Reference StringProperty, root.running_total

Replace:

        text: starting_number.text

with:

        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

Output

Figure 1 - App Startup Figure 2 - on_text Event Figure 3 - on_press Button Event

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 243955

the problem is that Label is bound with TextInput, and if you change Label as you are doing it the TextInput updates it again giving the feeling that it does not change. What you should do is change the text with the on_text event as shown below:

*.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

Upvotes: 1

Related Questions