我有一个基于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()
哪里'tabChanged'方法? – Leva7
对不起,我忘了在代码中添加该代码。现在应该有完整的代码。 – pBuch