In pyglet, the IncrementalTextLayout class from the pyglet.text.layout module is used to display multi-line formatted documents that can be edited incrementally. This is useful for situations like user-input fields or text editors.
To work with selections in the IncrementalTextLayout, you have the properties selection_start and selection_end at your disposal. These properties give the starting and ending positions of the current text selection, respectively.
Here's a simple example showing how you can access the selection_end property:
import pyglet from pyglet.window import key from pyglet.text import IncrementalTextLayout, FormattedDocument window = pyglet.window.Window() document = FormattedDocument("Hello, pyglet!") layout = IncrementalTextLayout(document, window.width, window.height, batch=pyglet.graphics.Batch()) @window.event def on_draw(): window.clear() layout.draw() @window.event def on_text(text): layout.insert_text(text) @window.event def on_key_press(symbol, modifiers): if symbol == key.RIGHT: layout.caret.move_right() print(f"Selection end is now at position: {layout.selection_end}") pyglet.app.run() In this example, every time you press the RIGHT arrow key, the caret (the text cursor) moves to the right, and the current position of selection_end is printed.
Remember, selection_start and selection_end give you indices into the document's text. When there's no active selection, both of these properties will simply reflect the current position of the caret.
scope chomp popup-blocker opencv .net-framework-version cdn sonarlint gson menu git-difftool