tags:

views:

42

answers:

1

The following code should return the width in pixels of the character 'a' in the default font; it doesn't:

import pygtk
gtk.require20()
import gtk

t = gtk.TextView()
print t.get_style().get_font().width("w") # Always returns -6
A: 

gtk.Style.get_font() is deprecated, as evidenced by the DeprecationWarning you get when you try to call it; I assume that's why it's not working. On my PyGTK there isn't any such function as gtk.Style.string_width().

You should use Pango:

import pygtk
pygtk.require20()
import gtk

t = gtk.TextView()
print t.create_pango_layout('w').get_pixel_size()
ptomato