Search code examples
pythongtk3pygobject

How to set Gtk.Entry text to right aligned in PyGObject?


I am beginner with PyGObject.

entry = Gtk.Entry()

Is there some method for setting the text alignment for Gtk.Entry in PyGObject ?


Solution

  • You need to use Gtk.Entry.set_alignment() and configure xalign to 1 (0 is left, 0.5 is centered, 1 is right).

    Here is an MCVE:

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    class EntryWindow(Gtk.Window):
    
        def __init__(self):
            super().__init__(title='Entry Widget')
            self.set_size_request(200, 100)
            self.entry = Gtk.Entry()
            self.entry.set_text("Hello World")
            self.entry.set_alignment(xalign=1)
            self.add(self.entry)
    
    win = EntryWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()
    

    GUI