Syntactic problems

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Syntactic problems

Francisco Gracia
I am exploring the possibilities of using *Tkinter* for a serious application mainly devoted to the handling of pure text.
 
As en exercise I have tried to rewrite in *Python* an interesting example that is unusually included in the manual page of *Tk* for the *font* component (*Tcl8.5.7/Tk8.5.7 Documentation > TkCmd > font > example*); this code works fine under *tclsh* and *wish*. What is does is getting a list of the font names available in the machine in which it is running and displaying nicely each of its items with a sample of the corresponding font.
 
I have performed most of my purpose and my *Python* script works. Along the way have learned a lot about *Tkinter/Tk/Tcl*, which was also a desired goal of the exercise. But the script does not work completely well, as I have also hit against an obstacle that I do not know how to overcome and seems to be rather deep. It is the following:
 
Some of the names of the font families returned by the operating system (be it Windows or Linux) are composed of two or more words (like *Times New Roman CE*). Now these multiword arguments are not easily accepted by *Tcl/Tk*, especially if they are referenced by some variable, so that for instance tags cannot be reconfigured this way in order to apply such fonts; or the size of sentences using them cannot be measured, two operations that are essential for the procedure.
 
I have learned that this is a real and inherent difficulty with *Tcl* (Harrison and McLennan, *Effective Tcl/Tk programming*: 34, 66); this is why the original code uses such a strange syntax like
 
    .t tag configure f[incr count] -font [list $family 10]
 
The function *list* of *Tcl* seems to be mainly dedicated to solve this odd problem with the dereferencing of multiword contents of variables (*family* in this case, which holds the name of the font). But apparently this does not work when *Tcl/Tk* are finally moved by *Tkinter* during the execution of the script or at least I have been unable to find a way to make it work in due form.
 
Which does not mean much, as I am not a programmer or, at most, I am a very unseasoned one. Can somebody who has had the same problem and solved it succesfully from *Tkinter* offer me (and perhaps us) his insight and describe the essentials of how to do it?
 
Many thanks in advance!

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Syntactic problems

Kevin Walzer-5
On 12/2/11 7:09 AM, Francisco Gracia wrote:

> Â
> Some of the names of the font families returned by the operating system
> (be it Windows or Linux) are composed of two or more words (like *Times
> New Roman CE*). Now these multiword arguments are not easily accepted by
> *Tcl/Tk*, especially if they are referenced by some variable, so that
> for instance tags cannot be reconfigured this way in order to apply such
> fonts; or the size of sentences using them cannot be measured, two
> operations that are essential for the procedure.

Multi-named fonts can be tricky.

> Â
> I have learned that this is a real and inherent difficulty with *Tcl*
> (Harrison and McLennan, *Effective Tcl/Tk programming*: 34, 66); this is
> why the original code uses such a strange syntax like
> Â
> Â Â Â  .t tag configure f[incr count] -font [list $family 10]
> Â
> The function *list* of *Tcl* seems to be mainly dedicated to solve this
> odd problem with the dereferencing of multiword contents of variables
> (*family* in this case, which holds the name of the font). But
> apparently this does not work when *Tcl/Tk* are finally moved by
> *Tkinter* during the execution of the script or at least I have been
> unable to find a way to make it work in due form.

I'm not quite clear on what problem you are experiencing here, as there
are no code snippets or Python errors outlined.

Are you just trying to understand the underlying Tcl idioms?

In Tcl, the [list $family 10] converts the multi-named font into a
single string that can be parsed by the font code. Tcl's [list] idiom is
very handy for things like this.

I'm not sure about how the Tkinter module interacts with Tcl at that low
level, but this Python code has always worked for me in displaying and
selecting a font from a listbox:

        self.fonts=list(tkFont.families())
         self.fonts.sort()

         for item in self.fonts:
             self.fontlist.insert(END, item)

         newfontlist = list(self.fontlist.get(0, END))

         if self.fontname in newfontlist:
             i = newfontlist.index(self.fontname)
             self.fontlist.see(i)
             self.fontlist.select_set(i)
             self.fontlist.select_anchor(i)
         else:
             pass

(This code is partly based on code in IDLE. The code in idleib is a bit
wooly, but snippets of it are quite useful for Tkinter samples.)

Hope this helps,
Kevin



> Â
> Which does not mean much, as I am not a programmer or, at most, I am a
> very unseasoned one. Can somebody who has had the same problem and
> solved it succesfully from *Tkinter* offer me (and perhaps us) his
> insight and describe the essentials of how to do it?
> Â
> Many thanks in advance!
>
>
> _______________________________________________
> Tkinter-discuss mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/tkinter-discuss


--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Syntactic problems

Michael Lange
Hi,

Thus spoketh Kevin Walzer <[hidden email]>
unto us on Fri, 02 Dec 2011 10:45:04 -0500:


> > Â Â Â  .t tag configure f[incr count] -font [list $family 10]
> > Â
> > The function *list* of *Tcl* seems to be mainly dedicated to solve
> > this odd problem with the dereferencing of multiword contents of
> > variables (*family* in this case, which holds the name of the font).
> > But apparently this does not work when *Tcl/Tk* are finally moved by
> > *Tkinter* during the execution of the script or at least I have been
> > unable to find a way to make it work in due form.
>
> I'm not quite clear on what problem you are experiencing here, as there
> are no code snippets or Python errors outlined.
>
> Are you just trying to understand the underlying Tcl idioms?

Maybe Francisco meant the following:

>>> l = Label(text='foo')
>>> l.pack()
>>> l.config(font='Helvetica 12')

this works, but:

>>> l.config(font='Bitstream Vera Sans 12')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1205, in configure
    return self._configure('configure', cnf, kw)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1196, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: expected integer but got "Vera"

I think the Tkinter equivalent to tcl's list command in this context is
to simply pass a sequence instead of a string to the font option:

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

just like one would do in other cases where Tk expects a list, as e.g.
canvas coords.

Best regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

It would seem that evil retreats when forcibly confronted.
                -- Yarnek of Excalbia, "The Savage Curtain", stardate
5906.5
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss