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

问题描述 投票:1回答:1

我是kivy和Python的新手。这是我的代码的片段:.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文件:

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

所以以下是两个问题:

  1. [尝试在on_enter函数中打印start1时,它正在打印在控制台中:

--我要打印kivy文本框值

  1. 当尝试在on_leave函数中打印start1时,空值为正在打印。我要打印kivy文本框值。
python kivy textinput
1个回答
0
投票

您将start1定义为StringProperty('Test'),并且在.kv文件中,您使用的start1: str(start)不会创建对TextInput对象的引用,而start1则指向TextInput对象的字符串表示形式。

要打印kivy文本框值,需要将start1定义为ObjectProperty(None)并在.kv文件start1: start中定义。然后,您可以使用self.start1.text简单地在相应的.py文件中引用TextInput值。

因此在.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)

在.kv文件中使用:

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

其余代码段保持不变,请注意缩进。

© www.soinside.com 2019 - 2024. All rights reserved.