How to I achieve keyboard focus behavior on a Button?

275 views
Skip to first unread message

John Boyd

unread,
Jun 18, 2022, 8:11:30 AM6/18/22
to Kivy users support
Hello,

I am trying to be able to tab from a textinput widget to a button. I read the docs but I am not successful.

I am able to tab and move easily between textinputs but not the button.

Any help will be greatly appreciated.

Best Regards,
John.

See file below:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.behaviors import FocusBehavior

class FocusButton(Button, FocusBehavior):
    pass

KV = '''
BoxLayout:
    orientation: "vertical"
    size_hint: .65, .65
    pos_hint: {'center_x': 0.5,'center_y': 0.5}
    padding: dp(48)
    spacing: dp(20)
    TextInput:
        id: in1
        hint_text: " First Input"
        multiline: False
        write_tab: False
    TextInput:
        id: in2
        hint_text: " Second Input"
        height: self.minimum_height
        multiline: False
        write_tab: False
        focus_next: but3
        #on_text_validate: but3.focus = True
    FocusButton:
        id: but3
        text: "Get Focus"
'''
root = Builder.load_string(KV)



class FocusApp(App):
    def build(self):
        return root

if __name__ == "__main__":
    FocusApp().run()

Elliot Garbus

unread,
Jun 18, 2022, 8:53:54 AM6/18/22
to kivy-...@googlegroups.com

You almost have it.

 

The behavior mixin must come first in the inheritance list.  From the docs, “The behavior class must always be _before_ the widget class. If you don’t specify the inheritance in this order, the behavior will not work because the behavior methods are overwritten by the class method listed first.

 

class FocusButton(FocusBehavior, Button):
   
pass

 

Adding an event to the FocusButton…

 

KV = '''

BoxLayout:
    orientation: "vertical"
    size_hint: .65, .65
    pos_hint: {'center_x': 0.5,'center_y': 0.5}
    padding: dp(48)
    spacing: dp(20)
    TextInput:
        id: in1
        hint_text: " First Input"
        multiline: False
        write_tab: False
    TextInput:
        id: in2
        hint_text: " Second Input"
        height: self.minimum_height
        multiline: False
        write_tab: False
        focus_next: but3
        #on_text_validate: but3.focus = True
    FocusButton:
        id: but3
        text: "Get Focus"
        on_focus: print(f'{self} has focus {self.focus}')

'''

You can see that the FocusButton gets focus.  There is no default behavior for a button in focus.  You will need to decide how you want the button to appear when in focus.  You could add an outline using the canvas or change the background color, or bold the text…

 

I suspect you will also want the button to fire when the user presses Enter.  You will also need to use the keyboard and add this to the Button.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/b0e660cb-cb64-4348-a0bc-ac5fdcc0f337n%40googlegroups.com.

 

John Boyd

unread,
Jun 18, 2022, 7:25:06 PM6/18/22
to Kivy users support

Perfect!

Thank you very much ElliotG!

Exactly the information I was looking for.

> I suspect you will also want the button to fire when the user presses Enter. You will also need to use the keyboard and add this to the Button.

That is exactly what I should have asked.  : )

However you have pointed me in the right direction.

Best Regards,

John.


Reply all
Reply to author
Forward
0 new messages