[Tkinter-discuss] Syntactic problems

Francisco Gracia fgragu023 at gmail.com
Sun Dec 4 13:36:10 CET 2011


2011/12/3 Michael Lange <klappnase at web.de> said:

Hi Francisco,
>


> The problem in you code is not the white space in the font name but the
> white space in the tag name ...
>

You are right, Michael, that was a mistake on my side. It was not a typo,
but a
confusion due to my bad knowledge of *Tcl*'s syntax and to my not
having given due consideration to the bussines at hand. Changing the name
of the tag to an only word eliminates the problem in this case and the
tag's handling becomes unproblematic.

... and then it worked as expected.
>

But with this not all problems are solved. Although the display
varies with the machine used, if one scans carefully
the whole output, one usually finds one or more lines where long font
names of several words invade the area of the samples display; this should
not happen and does not happen with Tcl's script. This is due to the
malfunction of the measuring function and in this case one cannot
avoid that the string to be measured has several words; on the contrary
this should be the normal case.

It seems however that I have been lucky enough to find an apparently
general solution. You can see it in the transcription of the whole script
that follows; for the moment this seems to be my final version.

I even consider the patch general because it also solves the difficulty of
your initial sample code, so that while

>>> c = 'Bitstream Vera Sans 12'
>>> l.config(font=c)

continues originating an error,

>>> c = '"[list Bitstream Vera Sans 12]"'
>>> l.config(font=c)

seems to work.

I want to thank you very much for your kindness and your interest.

Best regards

Francisco

# *Sampler.py*
# Display the names of the typographical fonts
# available in the present machine
# with a sample of the appearance of each one.

# Python version of the following *Tcl* code
# offered as example in the *font* manual page of *TkDocs*:

# pack [text .t -wrap none] -fill both -expand 1
# set count 0
# set tabwidth 0
# foreach family [lsort -dictionary [font families]] {
#    .t tag configure f[incr count] -font [list $family 10]
#    .t insert end ${family}:\t {} \
#            "This is a simple sampler\n" f$count
#    set w [font measure [.t cget -font] ${family}:]
#    if {$w+5 > $tabwidth} {
#        set tabwidth [expr {$w+5}]
#        .t configure -tabs $tabwidth
#    }
# }


from tkinter import *
from tkinter import font

# creation of the main window
master = Tk()

# creation of a text widget
tw = Text( master )
tw.config( wrap = NONE )
tw.pack( fill = BOTH, expand = 1 )

tw.insert( END, "Display of available fonts:\n\n" )

count = 0           # cardinal of the element
tabwidth = 0        # tabulator's position (in pixel)

# identification of the font in use (default)
f1 = tw.cget( 'font' )

# a measurement function to be applied later
# is a method of class *tkinter.font.Font*, so that
# it is good to get now the required instance of it
fo = font.Font( font = ( f1, 10 ) )

# let us get the alphabetically ordered list
# of all the font families available in this machine
sampler = sorted( list( font.families() ) )

# handling and display of its elements
for f in sampler :
    # the counter is updated
    count += 1
    # the provided font name is displayed
    # plus a separator and a tabulator
    tw.insert( END, f + ":\t" )
    # creation of an individual name for the tag
    # to be applied to this item's sample
    ftag = 'f' + str( count )
    # creation and typographical configuration of *ftag*
    tw.tag_config( ftag, font = ( f, 10 ) )
    # the sample text is finally tagged and displayed
    tw.insert( END, "This is a simple sampler\n", ftag )

    # nice columnar disposition of the output by
    # a clean and clever (re)configuration of the tabulator

    f = f + ':'
    if ' ' in f :       # the name has several words
        # (these strings of several words
        # are not well handled here by the complex *Tkinter/Tcl/Tk*;
        # fortunately it seems that the problem can be solved
        # by mimicking *Tcl*'s syntax for the occasion,
        # i.e. by flanking them with
        # the *magic* prefix:     '"[list '
        # and the *magic* suffix: ']"'      )
        f = '"[list ' + f + ']"'

    w = fo.measure( f )         # in pixel as unit
    if ( w + 5 ) > tabwidth :
        tabwidth = w + 5
        # this aligns all the lines as required
        tw.config( tabs = tabwidth )

if __name__ == '__main__' :
    master.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20111204/54706e7d/attachment.html>


More information about the Tkinter-discuss mailing list