[pygtk] Label a different font sizes

244 views
Skip to first unread message

Vláďa

unread,
Apr 16, 2008, 7:38:06 PM4/16/08
to py...@daa.com.au
Hi,

I have a question regarding labels and Pango.

I want to use different sizes of font, but unfortunately it doesn't
work. If I use

self.label = gtk.Label('<span size="14">Text 1</span>\n<span
size="10">Text 2</span>')

then only the first size definition (14) is taken into account. The
second line has the same font size.

Is this a feature, bug or my fault? Hot to create a label with different
font sizes in it?

Thank you
_______________________________________________
pygtk mailing list py...@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

John Finlay

unread,
Apr 16, 2008, 8:07:23 PM4/16/08
to Vláďa, py...@daa.com.au
Vláďa wrote:
> Hi,
>
> I have a question regarding labels and Pango.
>
> I want to use different sizes of font, but unfortunately it doesn't
> work. If I use
>
> self.label = gtk.Label('<span size="14">Text 1</span>\n<span
> size="10">Text 2</span>')
>
> then only the first size definition (14) is taken into account. The
> second line has the same font size.
>
> Is this a feature, bug or my fault? Hot to create a label with
> different font sizes in it?
First you have to tell the label to use Pango markup:

self.label.set_use_markup(True)

Then you have to specify a readable size since numerical sizes are in
thousandths of a point - see:

http://www.pygtk.org/docs/pygtk/pango-markup-language.html

John

Vláďa

unread,
Apr 17, 2008, 4:15:21 AM4/17/08
to John Finlay, py...@daa.com.au
Thank you for your reply. I know I have to use "set_use_markup(True)".
All of the Pango markup attributes work for me except of font size. At
the top of the page you are referring there is an example:

<span foreground="blue" size="100">Blue text</span>

So I think my code is correct. The problem is, that the first value
(size="14") in my example is also used for the second line, although I
specified new size (size="10"). The strange thing is that if I use
attributes like "large" or "small" then it works. Unfortunately my
English is rather weak, so I'm not sure if I can explain it clearly.
Here is a short sample code to show the problem:

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import pango

class HelloWorld:
def hello(self, widget, data=None):
print "Hello World"

def delete_event(self, widget, event, data=None):
print "delete event occurred"
return False

def destroy(self, widget, data=None):
print "destroy signal occurred"
gtk.main_quit()

def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.button = gtk.Button()
self.label = gtk.Label('<span size="20">Text 1</span>\n<span
size="10">Text 2</span>\n<span size="small">Text 3</span>')
self.label.set_use_markup(True)
self.button.add(self.label)
self.button.connect("clicked", self.hello, None)
self.button.connect_object("clicked", gtk.Widget.destroy,
self.window)
self.window.add(self.button)
self.window.show_all()

def main(self):
gtk.main()

if __name__ == "__main__":
hello = HelloWorld()
hello.main()

The button label has 3 lines, the first 2 have size 14, the last one is
small. But the second line should have size 10. What is wrong in my code?

Thank you


John Finlay napsal(a):

John Finlay

unread,
Apr 17, 2008, 4:34:55 AM4/17/08
to Vláďa, py...@daa.com.au
Vláďa wrote:
>
> So I think my code is correct. The problem is, that the first value
> (size="14") in my example is also used for the second line, although I
> specified new size (size="10"). The strange thing is that if I use
> attributes like "large" or "small" then it works. Unfortunately my
> English is rather weak, so I'm not sure if I can explain it clearly.
> Here is a short sample code to show the problem:
If I run your example code on my system (pygtk and gtk 2.12.0) the first
two lines are so small that they appear to be a dot. The third line is
readable. What version of pygtk and gtk are you using?

Neil Dugan

unread,
Apr 18, 2008, 11:54:40 PM4/18/08
to Vláďa, py...@daa.com.au, John Finlay
Vláďa wrote:
> Thank you for your reply. I know I have to use "set_use_markup(True)".
> All of the Pango markup attributes work for me except of font size. At
> the top of the page you are referring there is an example:
>
> <span foreground="blue" size="100">Blue text</span>
>
> So I think my code is correct. The problem is, that the first value
> (size="14") in my example is also used for the second line, although I
> specified new size (size="10"). The strange thing is that if I use
> attributes like "large" or "small" then it works. Unfortunately my
> English is rather weak, so I'm not sure if I can explain it clearly.
> Here is a short sample code to show the problem:
>
I used the line
self.label = gtk.Label('<span
size="20000">Texting</span>\n<span size="15000">Texting</span>\n<span
size="small">Text 3</span>')

Everything worked how it should
Ubuntu 7.10 (gutsy)
Gnome 2.10.1
Kernel 2.6.22-14-generic
Python 2.5.1

Regards Neil.

Vláďa

unread,
Apr 19, 2008, 12:45:07 PM4/19/08
to John Finlay, py...@daa.com.au
I¨m using 2.10.6 on Windows. I will update to 2.12, anyway I know
already know that the problem is caused by to small numbers for font
size. But it's strange that setting size=14 produces similar result as
font-size: 14px in HTML/CSS. This is what confused me. So the first 2
lines are on my system little bigger then normal font size.

Using values around 20000 works correctly. What unit is the font size
in? Thousandths of point?

Vlada

John Finlay napsal(a):

John Finlay

unread,
Apr 19, 2008, 3:13:26 PM4/19/08
to Vláďa, py...@daa.com.au
Vláďa wrote:
> I¨m using 2.10.6 on Windows. I will update to 2.12, anyway I know
> already know that the problem is caused by to small numbers for font
> size. But it's strange that setting size=14 produces similar result as
> font-size: 14px in HTML/CSS. This is what confused me. So the first 2
> lines are on my system little bigger then normal font size.
>
> Using values around 20000 works correctly. What unit is the font size
> in? Thousandths of point?
>
http://www.pygtk.org/docs/pygtk/pango-markup-language.html
Reply all
Reply to author
Forward
0 new messages