2016-05-14 109 views
0

我有一个基于TabbedPanel的Kivy应用程序。现在我想在更改为选项卡时自动对焦其中一个TextInputs。Kivy TabbedPanel自动对焦

当初始化应用程序,这个工程:

class FocusedTextInput(TextInput): 
    def on_parent(self, widget, parent): 
     self.focus = True 

但切换到另一个选项卡时,并回到第一位的,FocusedTextInput已经在其父所以回调不会抛出。无论何时我想要处理其他事件(例如TabbedPanelItem的click事件或其on_parent),FocusedTextInput的实例都是None,即使我安排了一个回调(例如10秒之后),并且输入可见且可通过其他函数(例如输入的focus_out处理程序)。

一旦用户更改了标签,有什么方法可以自动聚焦一个TextInput吗?

我期待着你的答案,并希望你的基维专家在那里。 ;-)


我的申请,这些部件可能会被interesing你: 这个类初始化标签(在这个例子中一个选项卡),并加载从KV文件中的内容。

class Applikation(App): 
    bezahlungProp = ObjectProperty(None) 
    #[...] 

    def build(self): 
     rootNode = TabbedPanel(do_default_tab=False) 
     rootNode.bind(current_tab=self.tabChanged) 

     bezahlungitem = TabbedPanelItem(text="Bezahlung") 
     self.bezahlungitem = bezahlungitem 
     self.bezahlung = Bezahlung() 
     rootNode.add_widget(bezahlungitem) 
     bezahlungitem.add_widget(self.bezahlung) 
     self.bezahlungProp = ObjectProperty(self.bezahlung) 
     self.bezahlung.add_widget(Builder.load_file("bezahlung.kv")) 
     # [...] 
     return rootNode 
    def tabChanged(self, instance, value): 
     Builder.sync() 
     value.content.init() 

这代表了一个标签:在bezahlung.kv的

class Bezahlung(BoxLayout): 
    ticketIDInp = ObjectProperty(None) 

    def on_parent(self, a, b): 
     Clock.schedule_once(self.on_parent_callback,10) 
    def on_parent_callback(self,b): 
     #this throws an error -> self.ticketIDInp is None 
     self.ticketIDInp.focus = True 

    def init(self): 
     #Application also fails here: 
     self.ticketIDInp.focus = True 

    def ticketIDInp_focus_out(self): 
     #I can use self.ticketIDInp.text here without problems 
     response = self.server.request("hookStatFromTicket",{"ticketID":self.ticketIDInp.text}) 
     [...] 

相关部分:

Bezahlung: 
    ticketIDInp: ticketIDInp 
    BoxLayout: 
     BoxLayout: 
      FocusedTextInput: 
       id: ticketIDInp 
       on_text_validate: root.ticketIDInp_focus_out() 
+0

哪里'tabChanged'方法? – Leva7

+0

对不起,我忘了在代码中添加该代码。现在应该有完整的代码。 – pBuch

回答

1

一种方法做你想做的是把CURRENT_TAB属性绑定。 这里是一个工作示例(第2个选项卡是一个带有想要的行为)

from kivy.app import App 
from kivy.uix.tabbedpanel import TabbedPanel 
from kivy.lang import Builder 

Builder.load_string(""" 

<Test>: 
    size_hint: .5, .5 
    pos_hint: {'center_x': .5, 'center_y': .5} 
    do_default_tab: False 

    TabbedPanelItem: 
     text: 'first tab' 
     Label: 
      text: 'First tab content area' 
    TabbedPanelItem: 
     id: tab2 
     text: 'tab2' 
     BoxLayout: 
      Label: 
       text: 'Second tab content area' 
      TextInput: 
       text: 'Button that does nothing' 
       focus: root.current_tab == tab2 # <--- this is it! also can be changed to "== self.parent.parent" 
""") 

class Test(TabbedPanel): 
    pass 


class TabbedPanelApp(App): 
    def build(self): 
     t = Test() 
     return t 

if __name__ == '__main__': 
    TabbedPanelApp().run() 
+0

正是我想要的,非常感谢你! – pBuch

+0

@pBuch - 没有问题的队友! –