python - Calculate size of textstring in pygtk pango for text fill -
i'm searching method pre calculate width of text string variable length python gtk/pango. want use adjusting text size automatically fill given space, gui can displayed different resolutions , text of labels or buttons or whatever should adjust given resolution use maximum possible font size without braking boundaries.
i have workaround now, slow method , think there must better:
def buttonschriftanpassen(self, aktiverbutton, inbox): '''function set fint size of directory/category buttons use max. amount of available space''' if inbox == true: aktiverbutton.show() gewolltebreite = aktiverbutton.get_parent().get_allocation()[2] gewolltehoehe = aktiverbutton.size_request()[1] maximalebreite = gewolltebreite elif aktiverbutton.get_label() == "home" or aktiverbutton.get_label() == "hauptmenue": #print aktiverbutton.get_label() gewolltebreite = aktiverbutton.size_request()[0] gewolltehoehe = aktiverbutton.size_request()[1] maximalebreite = gewolltebreite -(0.2*gewolltebreite)#* 1000 else: gewolltebreite = aktiverbutton.size_request()[0] gewolltehoehe = aktiverbutton.size_request()[1] maximalebreite = gewolltebreite # -(0.4*gewolltebreite)#* 1000 schriftgroessenfaktor = gewolltehoehe * 1000 templabel = gtk.label(aktiverbutton.get_label()) temppangoliste = pango.attrlist() xtemp = pango.attrsize(schriftgroessenfaktor, start_index=0, end_index=-1) temppangoliste.insert(xtemp) templabel.set_attributes(temppangoliste) abmessungentemplabel = templabel.get_layout().get_pixel_size() while abmessungentemplabel[0] > maximalebreite: schriftgroessenfaktor = schriftgroessenfaktor - 1000 xtemp = pango.attrsize(schriftgroessenfaktor, start_index=0, end_index=-1) temppangoliste.insert(xtemp) templabel.set_attributes(temppangoliste) abmessungentemplabel = templabel.get_layout().get_pixel_size() x = xtemp pangoliste = pango.attrlist() pangoliste.insert(x) aktiverbutton.child.set_attributes(pangoliste)
it builds temporary invisible label sets parameters , shrinks long measured extends bigger max. allowed size.
if there other function "filling" of text appreciate hear.
you render string pango , size.
example:
def get_pango_string_size(str, font_name=text_font): label = gtk.label() pango_layout = label.get_layout() pango_layout.set_markup(str) pango_font_desc = pango.fontdescription(font_name) pango_layout.set_font_description(pango_font_desc) return pango_layout.get_pixel_size()
(taken http://python.6.x6.nabble.com/getting-string-with-in-pixels-td1944346.html)
Comments
Post a Comment