Question

This seems like something that would be useful, and maybe I've missed it, but does a Kivy Textinput have a method that is equivalent to tkinter's Text.see(END)? I've looked at do_cursor_movement, but that just moves the cursor, not what is displayed.

Was it helpful?

Solution 2

I've found a bug with the cursor not being set properly vs the display on start up - in my example below, after adding the lines to the TextInput the cursor reads as (0, 100) even though the top of the text is actually displayed. This causes the Bottom button to do nothing - until you click in the TextInput (changing the cursor pos) or hit the Top button.

To see what I mean, just comment out Clock.schedule_once(lambda _: setattr(root.ids['ti'], 'cursor', (0, 0))).

I've tested this code as working with Kivy 1.8.1-dev (git: 1149da6bf26ff5f27536222b4ba6a874456cde6e) on Ubuntu 14.04:

import kivy
kivy.require('1.8.1')

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock

root = Builder.load_string('''
BoxLayout:
    orientation: 'vertical'

    BoxLayout:
        TextInput:
            id: ti

        Label:
            size_hint_x: None
            width: sp(80)
            text: str(ti.cursor)

    BoxLayout:
        size_hint_y: None
        height: sp(128)

        Widget

        Button:
            text: 'Top'
            on_press: ti.cursor = (0, 0)
        Button:
            text: 'Bottom'
            on_press: ti.cursor = (0, len(ti._lines) - 1)

        Widget
''')

class TestApp(App):
    def build(self):
        text = ''
        for i in xrange(100):
            text += 'Line %d\n' % (i + 1,)
        root.ids['ti'].text = text

        # fix the cursor pos
        Clock.schedule_once(lambda _: setattr(root.ids['ti'], 'cursor', (0, 0)))
        return root

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

OTHER TIPS

I've found that the problem is solved by working with the ScrollView that contains the TextInput. With the following in a .kv file

ScrollView:
    id: scroll1
    size_hint_x: 0.6
    TextInput:
        readonly: True
        id: main
        size_hint: 1, None
        height: max(self.minimum_height, scroll1.height)

Then all one needs to do is call self.ids['scroll1'].scroll_y = 0 This will scroll the TextInput to the bottom.

I'm not familiar to Kivy at all, nor did I test about my answer, but if I understand the docs correctly, by just setting the attribute cursor to the end of the line could do what you want.

Quoting:

cursor

Tuple of (row, col) values indicating the current cursor position.

You can set a new (row, col) if you want to move the cursor. The scrolling area will be automatically updated to ensure that the cursor is visible inside the viewport.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top