如何使用on_text_validate绑定两个或多个事件

时间:2018-02-27 17:57:10

标签: python kivy

我希望在按下输入TextInput小部件时它应该做两件事 -

  1. 更改屏幕(我正在使用屏幕管理器)

  2. 并搜索关键字

  3. 我知道使用on_text_validate,我们可以通过 -

    执行任一任务

    1 .root.manager.current='namesomething'

    2。root.function_which_has_search_algorithm()

    无论如何我可以使用on_text_validate来做两件事(changing_the_screen,calling_the_search_function)或者我是否必须使用其他技术?

    这里有一个示例代码:

    import kivy
    kivy.require("1.10.0")
    
    from kivy.app import App 
    from kivy.uix.screenmanager import ScreenManager,Screen 
    from kivy.lang import Builder
    
    Builder.load_file('screenswitch.kv')
    
    class MainScreen(Screen):
        def SelectWord(self):
            ''' some search
                       code'''
    
    class OtherScreen(Screen):
        pass
    
    sm = ScreenManager()
    sm.add_widget(MainScreen(name='main'))
    sm.add_widget(OtherScreen(name='other'))
    
    class ScreenSwitchApp(App):
        def build(self):
            return sm
    
    obj = ScreenSwitchApp()
    obj.run()
    

    继承我的kivy代码:

    <MainScreen>:
        BoxLayout:
            TextInput:
                text: "Search your word here"
                color: 1,1,1,1
                id: search_input
                width: 200
                size_hint: None, .20
                pos_hint: {"center_x": .5, "center_y": 0.5}
                multiline: False
                on_text_validate: root.SelectWord() # i want this to change screen also
    
    
    <OtherScreen>:
        BoxLayout:
            Button:
                text: 'back to main screen'
                on_press: root.manager.current='main'
    

2 个答案:

答案 0 :(得分:1)

一种可能的解决方案是创建一个调用这两个函数的新函数:

<强> *。PY

import kivy
kivy.require("1.10.0")

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager,Screen 
from kivy.lang import Builder

Builder.load_file('screenswitch.kv')

class MainScreen(Screen):
    def some_function(self):
        self.SelectWord()
        self.manager.current='other'

    def SelectWord(self):
        print("SelectWord")

class OtherScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(OtherScreen(name='other'))

class ScreenSwitchApp(App):
    def build(self):
        return sm

obj = ScreenSwitchApp()
obj.run()

<强> *。KV

<MainScreen>:
    BoxLayout:
        TextInput:
            text: "Search your word here"
            color: 1,1,1,1
            id: search_input
            width: 200
            size_hint: None, .20
            pos_hint: {"center_x": .5, "center_y": 0.5}
            multiline: False
            on_text_validate: root.some_function() # i want this to change screen also


<OtherScreen>:
    BoxLayout:
        Button:
            text: 'back to main screen'
            on_press: root.manager.current='main'

答案 1 :(得分:0)

        TextInput:
                id:qty_inp
                size_hint_x:.1
                multiline:False
                on_text_validate:root.update_price()
                on_text_validate:root.update_purchase

像上面的代码一样,您可以再添加一个on_text_validate:root.method()