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.

%MINIFYHTML3404e951f9c87ae5b7367937bdede36943%
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

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

The Secret Role of Karma in Mahabharata

The seeds of Mahabharata were sown long back Duryodhana or Dhritarashtra were born. The story of Kaurvas and Pandavas is only the second half of the cycle of Karma. So what is the first half?

The Real Super Food is free and all around you

Are you looking for some super food that can keep you healthy and strong forever? Do you want to stay free from all illness and health issues?

Latest Music Notations

Background Theme (BGM) – Mouna Ragam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Background Theme (BGM) from the 1986 Tamil movie Mouna Ragam in Western and Indian Notations.

Dil Kya Kare Jab Kisi Se – Julie – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Dil Kya Kare Jab Kisi Se from the 1975 Hindi movie Julie in Western and Indian Notations.

Albela Sajan Aayo Re – Hum Dil De Chuke Sanam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Albela Sajan Aayo Re from the 1999 Hindi movie Hum Dil De Chuke Sanam in Western and Indian Notations.

Chinni Chinni Aasa Chinnadaani Asa – Roja – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Chinni Chinni Aasa Chinnadaani Asa from the 1992 Telugu movie Roja in Western and Indian Notations.
Content Protection by DMCA.com