Reputation: 13
I want set the focus in textinput. at the begining the focus set correctly but when i go to the next screen and come back to initial screen the focus dont set correctly.
This is a app with a rfid lector, I want to read a code and select enter or exit
main.py
import kivy
kivy.require('1.10.0') # replace with your current kivy version !
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
class MyScreenManager(ScreenManager):
def __init__(self,**kwargs):
super(MyScreenManager,self).__init__()
class Menu1(Screen):
def __init__(self, **kwargs):
super(Menu1, self).__init__()
class Menu2(Screen):
def __init__(self, **kwargs):
super(Menu2, self).__init__()
Builder.load_file("main.kv")
class Fichaje(App):
def build(self):
return MyScreenManager()
if __name__ == '__main__':
Fichaje().run()
main.kv
#:kivy 1.10.0
#:import WipeTransition kivy.uix.screenmanager.WipeTransition
<MyScreenManager>:
#transition: WipeTransition()
Menu1:
id: menu1
Menu2:
id: menu2
<Menu1>:
name: "screen1"
BoxLayout:
orientation: 'vertical'
TextInput:
id: input1
size_hint_y: .1
multiline: False
focus: True
on_text_validate:
root.manager.current = "screen2"
BoxLayout:
<Menu2>:
name: "screen2"
BoxLayout:
Button:
text:"Entrada"
on_press:
root.manager.current = "screen1"
Button:
text:"Salida"
on_press:
root.manager.current = "screen1"
No error messages but the focus is not in the right site, Thanks
I change the code to simplify the error
Upvotes: 1
Views: 853
Reputation: 5405
In the example, there is not an attempt to change the focus. But I assume this was tried but it lost focus again.
The reason the text input looses focus again, is because it gets focused before the mouse or tap is released. The on_press
method, is followed by on_release
where the text input looses focus again.
To fix this you can just set the focus in the on_release
method instead.
The quickest is to only add one line of code to the kv file and change on_press
to on_release
.
root.manager.get_screen("screen1").ids["input1"].focus
This line can be different by using object property in screen1, for example. Or if you cannot use the on_release
method, maybe use clock to schedule a focus in some amount of time, and if the touch is still down, reschedule it.
But here is the quick fix.
<Menu2>:
name: "screen2"
BoxLayout:
Button:
text:"Entrada"
on_release:
root.manager.current = "screen1"
root.manager.get_screen("screen1").ids["input1"].focus = True
Upvotes: 1