Nirdesh Kumawat
Nirdesh Kumawat

Reputation: 406

Python : How to release `enter` key in python

I am using python-2.7 and kivy.When i run test.py then shows a form with 2 TextInput.
I am moving from one TextInput to another TextInput using Enter key.When i do not fill both TextInput and move using enter key.After that i focused ok button and i press Enter key then i call insert function and check it is blank or not.It it's blank then i set focus on Name TextInput using this function.

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

After focus name TextInput it calls root.on_enter_text_input() by default.So it move on next TextInput.But i want focus on name TextInput.

Can someone tell me how to stop root.on_enter_text_input() function at this time when i call insert function?

[![enter image description here][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()

Upvotes: 0

Views: 558

Answers (2)

ikolim
ikolim

Reputation: 16041

The solution is to add return True indicating that we have consumed the Enter key pressed and don’t want it to propagate any further. Please refer to the snippets below.

Snippets

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

Output

enter image description here

Upvotes: 1

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

You could do it like below

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

Upvotes: 0

Related Questions