[Tkinter-discuss] Syntactic problems

Francisco Gracia fgragu023 at gmail.com
Sat Dec 3 18:48:36 CET 2011


Many thanks to you both, Kevin and Michael for your quick and sensible
responses. My problems are more in the line suggested by Michael, although
his hint has not solved them, at least as I have tried to implement it.

In order for you to have all the information together, I transcribe the
original Tcl code and my *porting* of it to *tkinter*. I commented it for
my own use, in the process of conjecturing and confirming what the original
code was intended to perform.

If you run the original code with *tclsh* or *wish* and then my code with
*Idle*, you will see very clearly where the problem lies.

Kind regards

Francisco

# original Tcl code
# offered as example in the *font manual page* of TkDocs.
# Runs without problems with *tclsh* and *wish*

# 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
tabwidth = 0

# identification of the font being used
f1 = tw.cget( 'font' )

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

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

# handling and display of the elements
for f in sampler :
    # the counter is updated
    count += 1
    # display of the provided font name plus
    # a separator and a tabulator
    tw.insert( END, f + ":\t" )
    # creation of an individual name for the tag
    # that will be applied to the sample for this item
    ftag = f + str( count )

    # my (vain) efforts to solve the multiword problem
    if ' ' in f :
#        fm = "('" + f + "', '10')"   # as for Michael's hint
        fm = '[list ' + f + ']'
#        fm = f.replace( ' ', '_' )
#        fm = f.replace( ' ', '' )
        tw.tag_config( ftag, font = ( fm, 10 ) )
#        tw.tag_config( ftag, font = fm )
    else :
        # single word names work as expected
        tw.tag_config( ftag, font = ( f, 10 ) )
    # and the sample text:
    tw.insert( END, "This is a simple sampler\n", ftag )

    # nice columnar disposition by
    # a clever configuration of the tabulator
    w = fo.measure( f + ':' )
    if ( w + 5 ) > tabwidth :
        tabwidth = w + 5
        # this equalizes 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/20111203/78c89d34/attachment.html>


More information about the Tkinter-discuss mailing list