Skip to content

Commit 1d6a038

Browse files
committed
textinput: fix textinput focus to prevent crash when used at __init__ without beeing attached to root window. in that case, retry the next frame, and log a warning if it still not attached. closes kivy#315
1 parent 09105ee commit 1d6a038

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

kivy/uix/textinput.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def on_focus(instance, value):
103103

104104
import sys
105105

106+
from functools import partial
107+
from kivy.logger import Logger
106108
from kivy.utils import boundary
107109
from kivy.clock import Clock
108110
from kivy.cache import Cache
@@ -416,10 +418,20 @@ def on_touch_up(self, touch):
416418
#
417419
# Private
418420
#
419-
def on_focus(self, instance, value):
421+
def on_focus(self, instance, value, *largs):
420422
win = self._win
421423
if not win:
422424
self._win = win = self.get_root_window()
425+
if not win:
426+
# we got argument, it could be the previous schedule
427+
# cancel focus.
428+
if len(largs):
429+
Logger.warning('Textinput: '
430+
'Cannot focus the element, unable to get root window')
431+
return
432+
else:
433+
Clock.schedule_once(partial(self.on_focus, self, value), 0)
434+
return
423435
if value:
424436
keyboard = win.request_keyboard(self._keyboard_released, self)
425437
self._keyboard = keyboard

0 commit comments

Comments
 (0)