0

Similar/relevant links that didn't help much:

  1. How to get a text input box to display with Kivy?
  2. https://kivy.org/doc/stable/api-kivy.uix.textinput.html
  3. Getting started with Kivy: getting user input using Kivy

I have been at this for several hours and I have found questions similar to mine but nothing has actually worked. Here's what I'm trying to do: Create a button that when pressed, pulls up a text input box and then displays whatever you type on the button after a short string. E.G. Button starts like: "LP: " You enter text: "4000" Button now shows: "LP: 4000" How would I accomplish this? If that's not totally possible I'd also be okay with just getting the input after hitting the button. I can't even seem to get that far. Very new to Kivy and fairly new to Python. Button code (KV File):

    <FloatLayout>:
        Button:
           name: 'LP'
           id: LP
           text: "LP: 4000"
           size_hint: 0.14, 0.15
           pos_hint: {"left": 1, "top": 0.8105}

Class (Py File):

    class LPInput(Widget):
        pass

Code for the input (KV File):

    <LPInput>:
        size_hint: 0.14, 0.15
        pos_hint: {"left": 1, "top": 0.8105}

        TextInput:
            id: lifepoint
            text: ""

        Label:
            id: currlp #not sure this is doing anything
            text: "LP: "

I've written some other coding pieces to attempt to create this via different methods but in my frustration I saved over my file that was holding those so this is all I have at the moment.

Tim
  • 10,459
  • 4
  • 36
  • 47
Kara
  • 1
  • 1

1 Answers1

0

The solution below is assuming LPInput is the root widget.

<LPInput>:
    size_hint: 0.14, 0.15
    pos_hint: {"left": 1, "top": 0.8105}

    TextInput:
        id: lifepoint
        text: ""

    Label:
        id: currlp #not sure this is doing anything
        text: "LP: "

<FloatLayout>:
    Button:
       name: 'LP'
       id: LP
       text: "LP: "
       size_hint: 0.14, 0.15
       pos_hint: {"left": 1, "top": 0.8105}
       on_release:
           self.text = "LP: " + app.root.ids.lifepoint.text

Kv language » Rule context

There are three keywords specific to Kv language:

app: always refers to the instance of your application.

root: refers to the base widget/template in the current rule

self: always refer to the current widget

Example

main.py

from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
#:kivy 1.11.0
FloatLayout:

    TextInput:
        id: lifepoint
        text: ""
        size_hint: 0.14, 0.15
        pos_hint: {"left": 1, "top": 0.8105}

    Button:
        name: 'LP'
        id: LP
        text: "LP: "
        size_hint: 0.14, 0.15
        pos_hint: {"right": 1, "top": 0.8105}
        on_release:
            self.text = "LP: " + root.ids.lifepoint.text
'''))

Output

Img01 Img02

ikolim
  • 15,721
  • 2
  • 19
  • 29