I am working on a GTK4 application using Python, and I am trying to justify (fill) the text in a Gtk.TextView. While the text is justified properly for most lines, the last line of the text block is not fully justified (it aligns to the left instead).
Here is the relevant code snippet:
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
class JustifyWindow(Gtk.Application):
def __init__(self):
super().__init__(application_id="com.example.JustifiedText")
self.connect("activate", self.on_activate)
def on_activate(self, app):
window = Gtk.ApplicationWindow(application=app)
window.set_title("GTK4 Justified Text")
window.set_default_size(500, 300)
textview = Gtk.TextView()
textview.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
textview.set_justification(Gtk.Justification.FILL)
textview.set_hexpand(True)
textview.set_vexpand(True)
textview.set_editable(False)
textview.set_cursor_visible(False)
textview.set_margin_top(20)
textview.set_margin_bottom(20)
textview.set_margin_start(20)
textview.set_margin_end(20)
buffer = textview.get_buffer()
# Content
parts = [
"This is the first line that should be justified.",
"This is the second line, also part of the same justified block.",
"And here is the third line."
]
buffer.set_text('\n'.join(parts))
window.set_child(textview)
window.present()
app = JustifyWindow()
app.run()
The Gtk.TextView is set to use Gtk.Justification.FILL, which works as expected for all lines except the last one. The last line remains left-aligned, and I would like it to also be fully justified.
Is there a way to force the last line of a Gtk.TextView to be justified in GTK4? If not, are there any workarounds or alternative approaches to achieve this behavior?
Any help or guidance would be greatly appreciated!