0

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!

2
  • I'm not sure, but I believe that the last line of a paragraph is never justified. This is probably for visibility reasons. I don't think there's any way around it. Commented May 13 at 20:47
  • FYI - Just so you don't chase the same dead end: You can accomplish what you're asking for on the web in CSS using text-align-last: justify; Since you can apply some CSS to Gtk widgets I knocked together some test code. Unfortunately, TextView doesn't have a text-align-last property. I tried several other properties to prove that the code worked, and color did. Incidentally font-weight: bold; caused the TextView to x-ray through all of my windows and show my desktop wallpaper. I could even drag the window around and reveal different parts of my desktop! Commented Sep 30 at 4:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.