简体   繁体   English

如何打印在.kv(kivy)中输入的TextInput值,以打印在.py文件中?

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

I am new to kivy and Python.我是 kivy 和 Python 的新手。 This is a snippet of my code: .py file:这是我的代码片段: .py 文件:

    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: .kv 文件:

<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:所以以下是2个问题:

  1. When trying to print start1 in on_enter function, it is printing this in the console:当尝试在 on_enter function 中打印 start1 时,它正在控制台中打印:

-<--kivy.uix.textinput.TextInput object at 0x00000240AC1E0208-->- I want to print the kivy textbox value -<--kivy.uix.textinput.TextInput object at 0x00000240AC1E0208-->- 我想打印 kivy 文本框值

  1. When trying to print start1 in on_leave function, null value is being printed.I want to print the kivy textbox value.尝试在 on_leave function 中打印 start1 时,正在打印 null 值。我想打印 kivy 文本框值。

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.您将start1定义为StringProperty('Test')和 in.kv 文件,您正在使用 start1 start1: str(start)它不会创建对 TextInput object 的引用,而 start1 是指 TextInput object 的字符串表示形式。

To print the kivy textbox value, you need to define start1 as ObjectProperty(None) and in.kv file start1: start .要打印 kivy 文本框值,您需要将 start1 定义为 ObjectProperty(None) 和 in.kv 文件 start1 start1: start Then you can simply refer to TextInput value in corresponding.py file using self.start1.text .然后您可以使用self.start1.text简单地引用对应.py 文件中的 TextInput 值。

So in.py file use:所以在.py文件中使用:

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:在.kv 文件中使用:

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

Rest of the code snippet remains same and please take care of indentation.代码片段的 Rest 保持不变,请注意缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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