通过回车键检测失去焦点(PythonKivy)

2024-09-26 22:45:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想提高Python Kivy程序的可用性。在下面的程序示例中,我希望允许用户操作它,即使他们没有鼠标。(通过键盘输入。) (1) 在第一个对话框(MyLayout2)中,用户可以轻松地在文本框中输入内容,因为文本框具有焦点 (2) 按键盘键进入下一个对话框(MyLayout1) (3) 按键盘回车键转到(1)(再次按MyLayout2) 但是在第二个(1)中,在(3)之后,文本框的焦点丢失。你知道怎么处理这个问题吗?你知道吗

你知道吗主.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

class myApp(App):
    def build(self):  
        self.anschoi = MyScreen(name = 'anschoi') 
        sm.add_widget(self.anschoi)
        sm.current = 'anschoi'
        return sm

if __name__ == '__main__': 
    myApp().run()

你知道吗my.kv你知道吗

<MyScreen>:
    BoxLayout:
        orientation: 'vertical'
        padding: 10,40,10,40 
        spacing: 40 

<MyLayout1>:
    Button1:
        id: btn1
        text: 'OK or ENTER key'
        on_release: root.parent.button1_clicked()  


<MyLayout2>:
    txtinput: txtinput
    orientation: 'vertical'
    TextInput:
        id: txtinput
        text: ''
        multiline: False
        focus: True
    button2:
        id:Button2
        text: 'OK or ENTER key'
        on_release: root.parent.button2_clicked()  

<Button0@Button>:
<Button1@Button>:
<button2@Button>:

Tags: fromimportselfnoneaddnumberifdef
1条回答
网友
1楼 · 发布于 2024-09-26 22:45:23

这和你以前的问题完全一样。改变一下:

Window.bind(on_key_down=self._keydown)

收件人:

Window.bind(on_key_up=self._keydown)

同样,要避免on_key_up事件(几乎总是在on_key_down事件之后发生)从TextInput窃取焦点。你知道吗

相关问题 更多 >

    热门问题