输入键检测丢失焦点(Python-Kivy)

我想提高我的 Python-Kivy 程序的可用性。在下面的程序示例中,我想让用户即使没有鼠标也可以操作它。(通过键盘输入。)(1)在第一个对话框(MyLayout2)中,用户在文本框中轻松输入,因为文本框有焦点(2)按 Enter 键盘键转到下一个对话框(MyLayout1)(3)按 Enter 键盘键转到(1)(再次MyLayout2)但是在第2(1)中,在(3)之后,Textbox的焦点丢失了。知道如何解决这个问题吗?


Main.py

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout

from kivy.properties import NumericProperty

from kivy.uix.gridlayout import GridLayout

from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.uix.textinput import TextInput

from kivy.core.window import Window


sm = ScreenManager()

class MyLayout1(BoxLayout):

    pass


class MyLayout2(BoxLayout):

    pass


class MyScreen(Screen):

    dialog_number = 0

    def __init__(self, **kwargs):

        super(MyScreen, self).__init__(**kwargs)

        self.gridlayout = None

        self.gridlayout = MyLayout2()

        self.add_widget(self.gridlayout)

        Window.bind(on_key_down=self._keydown)


    def _keydown(self,*args):   

        if (args[2] == 40):

            if self.dialog_number == 0:

                self.button2_clicked()

            elif self.dialog_number == 1:

                self.button1_clicked()


    def _create_layout(self):

        if self.gridlayout is not None:

            self.remove_widget(self.gridlayout)

        self.add_widget(self.gridlayout)


    def button1_clicked(self, *args):

        if self.gridlayout is not None:

            self.remove_widget(self.gridlayout)

        self.gridlayout = MyLayout2()

        self.add_widget(self.gridlayout)

        self.dialog_number = 0


    def button2_clicked(self, *args):

        if self.gridlayout is not None:

            self.remove_widget(self.gridlayout)

        self.gridlayout = MyLayout1()

        self.add_widget(self.gridlayout)

        self.dialog_number = 1


    def find_instance(self, layout):

        c = None

        for c in list(self.children):

            if isinstance(c, layout): 

                break

        return c


慕标5832272
浏览 232回答 1
1回答

哆啦的时光机

这与您之前的问题完全相同。只是改变:Window.bind(on_key_down=self._keydown)到:Window.bind(on_key_up=self._keydown)同样,为了避免on_key_up事件(几乎总是在on_key_down事件之后)从你的TextInput.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python