简体   繁体   中英

How to print TextInput value entered in .kv (kivy), to be printed in .py file?

I am new to kivy and Python. This is a snippet of my code: .py file:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.properties import ObjectProperty, StringProperty
    from kivy.uix.popup import Popup
    from kivy.uix.label import Label
    from database import DataBase    

class IpickupWindow(Screen):
n = ObjectProperty(None)
start1 = StringProperty('Test')
current = ""
pop = StringProperty("")
def on_enter(self, *args):
    name = db.curr_user(self.current)
    self.n.text = "Hi, " + name
    print(self.start1)


def on_leave(self, *args):
    print(self.start1)
    #print(self.start1.text)

def btn(self):
    popup = CustomPopup()
    print(self.pop)
    popup.open()

.kv file:

<IpickupWindow>:
name:"Ipick"
n:n
start1: str(start)

FloatLayout:
    cols: 1

    FloatLayout:
        size: root.width, root.height/2

        Label:
            id: n
            pos_hint:{"x": 0.0, "top":1.0}
            size_hint:0.8, 0.2

        Label:
            pos_hint:{"x": 0.1, "top":0.9}
            size_hint:0.8, 0.2
            text: "Please provide the below details to find the best ride for you: "
        Label:
            pos_hint:{"x": 0.0, "top":0.75}
            size_hint:0.4, 0.2
            text: "Start Location: "
        TextInput:
            id: start
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.4 , "top":0.7}
            size_hint: 0.3, 0.1

        Label:

            pos_hint:{"x": 0.0, "top":0.55}
            size_hint:0.4, 0.2
            text: "End Location: "
        TextInput:
            id: end
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.4 , "top":0.5}
            size_hint: 0.3, 0.1

        Label:
            pos_hint:{"x": 0.0, "top":0.35}
            size_hint:0.4, 0.2
            text: "Time frame: "
        TextInput:
            id: time
            font_size: (root.width**2 + root.height**2) / 13**4
            multiline: False
            pos_hint: {"x": 0.4 , "top":0.3}
            size_hint: 0.3, 0.1

    Button:
        pos_hint:{"x":0.4, "top": 0.1}
        size_hint:0.2,0.1
        text: "Submit"
        on_press: root.btn()

so the following are the 2 problems:

  1. When trying to print start1 in on_enter function, it is printing this in the console:

-<--kivy.uix.textinput.TextInput object at 0x00000240AC1E0208-->- I want to print the kivy textbox value

  1. When trying to print start1 in on_leave function, null value is being printed.I want to print the kivy textbox value.

You are having start1 defined as StringProperty('Test') and in.kv file you are using start1: str(start) which does not create a reference to TextInput object rather start1 refers to string representation of TextInput object.

To print the kivy textbox value, you need to define start1 as ObjectProperty(None) and in.kv file start1: start . Then you can simply refer to TextInput value in corresponding.py file using self.start1.text .

So in.py file use:

class IpickupWindow(Screen):
    n = ObjectProperty(None)
    start1 = ObjectProperty(None)
    current = ""
    pop = StringProperty("")
    def on_enter(self, *args):
        name = db.curr_user(self.current)
        self.n.text = "Hi, " + name
        print(self.start1.text)

In.kv file use:

<IpickupWindow>:
    name:"Ipick"
    n:n
    start1: start

Rest of the code snippet remains same and please take care of indentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM