Home Code How to left align text in a GTKLabel in Python?

How to left align text in a GTKLabel in Python?

I had a very difficult time trying to search the web for how to left align text in a GTKLabel in Python. So finally when I figured it out from some C language code, I thought it would benefit others Googling for code snippet to left align a text in GTK Label in Python.

The difficulty arises from the fact that by default the contents are center aligned in GTKLabel. I use Glade to build GUI applications in Python. So below is the code snippet if you want to left algin text in a GTKLabel in Python.

label = Gtk.Label("My Text")
label.set_alignment(0,0)

Yes, it is as simple as calling the set_alignment method. It takes two argument, one is horizontal alignment and the other is vertical aligment. So for left aligning the text, we are interested in the first argument which is horizontal alignment.

Hence, by passing 0 as the first argument, we make sure that the text in GTK Label is left aligned. If you want some small padding instead of aligning it to complete left, the code could be modified to have some minimum left padding as follows.

label.set_alignment(0.05,0)

On the other hand, if you want to right align the text in GTK Label, then the code would be as follows.

label.set_alignment(1,0)

The above line will right align the text. As you can guess, the code for the default center aligning of text then would be as follows:

label.set_alignment(0.5,0)

Similarly you can adjust the second parameter to modify vertical alignment of text to top, middle or bottom of the label.

Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,258FansLike
7FollowersFollow
133FollowersFollow
380FollowersFollow

Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?

The individual human is smart, but the human species is dumb

The smart individual human has to clean up the mess created by his dumb species. Species polluted the planet, individuals are cleaning it up.

What if Dinosaurs did not go extinct 65 million years ago?

Imagine a world where dinosaurs did not go extinct, because that asteroid missed colliding with Earth. How would have life evolved then?
Content Protection by DMCA.com