【问题标题】:Kivy TabbedPanel AutofocusKivy TabbedPanel 自动对焦
【发布时间】:2016-05-14 10:32:47
【问题描述】:

我有一个基于TabbedPanel 的 Kivy 应用程序。现在我想在切换到选项卡时自动聚焦其中一个 TextInputs。

在初始化应用程序时,这是可行的:

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

但是当切换到另一个选项卡并返回第一个选项卡时,FocusedTextInput 已经在其父级上,因此不会引发回调。每当我想处理其他事件(例如,TabbedPanelItem 的点击事件或它的 on_parent)时,FocusedTextInput 的实例为 None,即使我安排了回调(例如 10 秒后)并且输入是可见的并且可以通过其他函数(例如输入的 focus_out 处理程序)。

有没有办法在用户更改标签后自动聚焦一个 TextInput?

我期待您的回答,并希望您的 kivy 专家在那里。 ;-)


我的应用程序的这些部分可能会让您感兴趣: 此类初始化选项卡(本示例为一个选项卡)并从 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()

这代表一个标签:

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.kv 的相关部分:

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

【问题讨论】:

  • tabChanged 方法在哪里?
  • 对不起,我忘了在代码中添加它。现在应该有完整的代码了。

标签: python kivy


【解决方案1】:

一种方法是绑定 current_tab 属性。 这是一个工作示例(第二个选项卡是具有所需行为的选项卡)

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

【讨论】:

  • @pBuch - 没问题,伙计!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多