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)Code language: Python (python)

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)Code language: Python (python)

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)Code language: Python (python)

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

Download HitXP Mobile App

Get it on Google Play
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

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Power of Democracy: How Free Societies Foster Innovation in Science and Technology

Democracy allows freedom of speech and thought, boosting creativity and innovation. This leads to scientific breakthroughs and technological advancements.

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Latest Music Notations

Jai Shri Ram – Adipurush – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Jai Shri Ram from the 2023 Hindi movie Adipurush in Western and Indian Notations.

Sojugada Soojumallige – Garuda Gamana Vrishabha Vahana – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Sojugada Soojumallige from the 2021 Kannada movie Garuda Gamana Vrishabha Vahana in Western and Indian Notations.

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.
Content Protection by DMCA.com