【问题标题】:Python : How to release `enter` key in pythonPython:如何在 python 中释放`enter`键
【发布时间】:2018-10-29 17:53:25
【问题描述】:

我正在使用python-2.7kivy。当我运行test.py 时,会显示一个带有2 TextInput 的表单。
我正在使用Enter 键从一个TextInput 移动到另一个TextInput。当我没有同时填写TextInput 并使用回车键移动时。之后我focusedok 按钮并按下Enter 键然后我打电话insert 函数并检查它是否为空白。它是空白的然后我使用此函数在Name TextInput 上设置focus

def insert(self):
    if self.name.text == "":
        self.name.focus = True

焦点后name TextInput 默认调用root.on_enter_text_input()。所以它继续下一个TextInput。但我想关注name TextInput。

有人可以告诉我当我调用 insert 函数时如何停止 root.on_enter_text_input() 函数吗?

[![在此处输入图片描述][1]][1]

test.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 300)


class User(Screen):
    name = ObjectProperty(None)
    class1 = ObjectProperty(None)
    #cls = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)
        Clock.schedule_once(self.name_focus, 1)

    def name_focus(self, *args):
        self.name.focus = True

    def on_enter_text_input(self):
        self.class1.focus = True

    def on_enter_text_input1(self):
        self.postUser.focus = True
        self.postUser.background_color = [0.5, 0.5, 0.5, 1]

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:  # 40 - Enter key pressed
            self.postUser.focus = False
            self.postUser.background_color = [0, 0, 1, 0.5]
            self.insert()


    def insert(self):
        if self.name.text == "":
            self.name.focus = True

class Test(App):

    def build(self):
        return self.root


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

test.kv

#:kivy 1.10.0

User:
    name: name
    class1:class1
    postUser : postUser
    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            padding: 20, 20
            spacing: 10, 10

            Label:
                text: "Name"
                text_size: self.size
                valign: 'middle'
            TextInput:
                id:name
                multiline: False
                text_size: self.size
                on_text_validate: root.on_enter_text_input()
            Label:
                text: "Class"
                text_size: self.size
                valign: 'middle'
            TextInput:
                id:class1
                multiline: False
                text_size: self.size
                on_text_validate: root.on_enter_text_input1()

        GridLayout:
            cols: 2
            padding: 0, 0
            spacing: 5, 0
            size_hint: .5, .35
            pos_hint: {'x': .25, 'y': 0}

            Button:
                id:postUser
                size_hint_x: .5
                text: "Ok"
                focus: False
                on_release:
                    root.insert()
                    root.dismiss()

【问题讨论】:

    标签: python-2.7 kivy


    【解决方案1】:

    解决方案是添加return True,表示我们已经消耗了按下的 Enter 键并且不希望它进一步传播。请参考下面的sn-ps。

    片段

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:  # 40 - Enter key pressed
            self.postUser.focus = False
            self.postUser.background_color = [0, 0, 1, 0.5]
            self.insert()
            return True
    

    输出

    【讨论】:

      【解决方案2】:

      你可以像下面那样做

      class User(Screen):
          name = ObjectProperty(None)
          class1 = ObjectProperty(None)
          #cls = ObjectProperty(None)
      
          def __init__(self, **kwargs):
              super(User, self).__init__(**kwargs)
              self.initial_focusing = False
              Window.bind(on_key_down=self._on_keyboard_down)
              Clock.schedule_once(self.name_focus, 1)
      
          def name_focus(self, *args):
              self.name.focus = True
      
          def on_enter_text_input(self):
              if self.initial_focusing:
                 self.initial_focusing = False
              else:
                 self.class1.focus = True
      
          def on_enter_text_input1(self):
              self.postUser.focus = True
              self.postUser.background_color = [0.5, 0.5, 0.5, 1]
      
          def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
              if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:  # 40 - Enter key pressed
                  self.postUser.focus = False
                  self.postUser.background_color = [0, 0, 1, 0.5]
                  self.insert()
      
      
          def insert(self):
              if self.name.text == "":
                  self.initial_focusing = True
                  self.name.focus = True
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-03
        • 2023-04-05
        • 2021-11-13
        • 1970-01-01
        • 2018-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多