我想提高我的 Python-Kivy 程序的可用性。在下面的程序示例中,我想让用户即使没有鼠标也可以操作它。(通过键盘输入。)(1)在第一个对话框(MyLayout2)中,用户在文本框中轻松输入,因为文本框有焦点(2)按 Enter 键盘键转到下一个对话框(MyLayout1)(3)按 Enter 键盘键转到(1)(再次MyLayout2)但是在第2(1)中,在(3)之后,Textbox的焦点丢失了。知道如何解决这个问题吗?Main.pyfrom kivy.app import Appfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.properties import NumericPropertyfrom kivy.uix.gridlayout import GridLayoutfrom kivy.uix.screenmanager import ScreenManager, Screenfrom kivy.uix.textinput import TextInputfrom kivy.core.window import Windowsm = ScreenManager()class MyLayout1(BoxLayout): passclass MyLayout2(BoxLayout): passclass 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
1 回答
哆啦的时光机
TA贡献1779条经验 获得超6个赞
这与您之前的问题完全相同。只是改变:
Window.bind(on_key_down=self._keydown)
到:
Window.bind(on_key_up=self._keydown)
同样,为了避免on_key_up
事件(几乎总是在on_key_down
事件之后)从你的TextInput
.
添加回答
举报
0/150
提交
取消