在文本框中更改文本时,动态地从Textinput读取文本?

时间:2017-09-19 14:54:23

标签: python kivy kivy-language

我是kivy的新手,我正在尝试在python中使用kivy编写一个应用程序,我在某一点上受到了打击,我必须在textInput中读取文本,只要它被更改并基于我想要实现我的按钮的功能 - 我已经完成了所有文档,但我无法弄清楚如何做到 - 任何身体都可以告诉我如何解决这个问题或者我错过了什么?

from __future__ import print_function
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.core.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import *
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
import walascan
from kivy.clock import Clock
import os


kv = """
<KartScan>:
    IntroScreen:

<IntroScreen@Screen>:
    orientation: 'horizontal'
    name: 'introscreen'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'index.png'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        BoxLayout:
            orientation:'horizontal'
            size_hint: .5, .1
            canvas:
                Color:
                    rgb: 1, 1, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            spacing: 20
            pos_hint: {'center_x':.8, 'center_y': .8}
            AnchorLayout:
                anchor_x: 'left'
                size_hint_x: .5
                TextInput:
                    id: waybill
                    width: 20
                    text: "Enter Waybill No."
                    multiline: False
                    height: self.minimum_height
                    size_hint_y: None
                    font_size: 30
                    focus: True
                    on_text_validate: app.on_waybill()
            AnchorLayout:
                anchor_x: 'right'
                size_hint_x: None
                Button:
                    size_hint: None, None
                    height: 50
                    width: self.texture_size[0]
                    padding: 10, 10
                    text: "Add"
                    on_press:app.buttonClicked()
                    on_release: root.current = 'mainpage'
            AnchorLayout:
                anchor_x: 'right'
                size_hint_x: None
                Button:
                    size_hint: None, None
                    height: 50
                    width: self.texture_size[0]
                    padding: 10, 10
                    text: "Compare"
                    on_press:app.buttonClicked()
                    on_release: root.current = 'mainpage'
"""

Builder.load_string(kv)

waybill = TextInput(text="Enter Waybill No.", multiline=False)



class KartScan(FloatLayout):
    def __init__(self, **kwargs):
        super(KartScan, self).__init__(**kwargs)
        self.register_event_type('on_text_validate')

    def on_text(self, *args):
        print('new value is ', waybill.text)

    def on_text_validate(self):
        pass

    def on_focus(self, obj, focused):
        if not focused:
            self.dispatch('on_text_validate')


class KartScanApp(App):

    def build(self):
        return KartScan()

    def buttonClicked(self):
        popup = Popup(title='Result',
                      content=Label(text=self.on_waybill()),
                      size_hint=(None, None), size=(100, 100))
        popup.bind()
        popup.open()

    def getwlbtstate(self):
        return walascan.mainAntennas()

    def on_waybill(self):
        waybill.bind(text=KartScan.on_text_validate)
        # popup = Popup(title='Result',
        #               content=Label(text=waybill.text),
        #               size_hint=(None, None), size=(100, 100))
        # popup.bind()
        # popup.open()
        return waybill.text

if __name__ == '__main__':
    KartScanApp().run()

2 个答案:

答案 0 :(得分:3)

kv文件

TextInput:
    on_text: print(self.text)

当文本发生变化时,您可以使用on_text来调用函数。我认为有时甚至会在没有发生变化的情况下触发。

答案 1 :(得分:3)

TextInput具有text属性,并且在kivy中,所有properties都有关联的on_xxx事件,您可以绑定它们以对它们作出反应,因此在阅读文档时,即使每次你看到的东西被定义为Property(或者它的任何子类,如StringProperty NumericProperty , ObjectProperty , AliasProperty`)时,都没有明确提到它们。 / p>

TextInput:
    on_text: do_something(self.text)

或在python中

t = TextInput(text='', on_text=do_something)

t = TextInput(text='')
t.bind(on_text=do_something)