[Tkinter-discuss] Still don't understand ttk styling

Michael Lange klappnase at web.de
Fri Dec 7 14:14:17 CET 2012


Hi John,

On Thu, 6 Dec 2012 15:07:35 -0700 (MST)
"John W. Shipman" <john at nmt.edu> wrote:

> I've read and reread everything I can find about ttk, both in
> the Tcl docs and the Python 2.7 library docs, and I still have
> no idea how styling really works.
> 
> Just to focus the discussion, here's a very simple task: change
> the font that appears on the tabs in the ttk.Notebook widget.
> 
> The only way I found to do this is to change the root style's
> default like this:
> 
>      s = ttk.Style()
>      s.configure('.', font=('Helvetica', 16, 'bold'))
> 
> Is this the only way I can style the tab font?  If not, can
> someone please explain how you figure out where Notebook
> gets its font, and how to change it?

I often find it hard to figure out thing like these, too. I think the best
source is usually reading the .tcl files where widget and style defaults
are defined.

As far as I understand, ttk as well as standard tk uses the
set of tk standard fonts by default, as returned by tkFont.names () ,
here:

('TkCaptionFont', 'font4143566764', 'TkSmallCaptionFont',
'TkTooltipFont', 'TkFixedFont', 'TkHeadingFont', 'TkMenuFont',
'TkIconFont', 'TkTextFont', 'TkDefaultFont')

I don't have the tk source code at hand, I would guess these are
hard-coded there, so that Text uses kTextFont and so on.

It looks like ttk overrides these defaults in ttk/fonts.tcl first,
then defines which font to use for styles in ttk/defaults.tcl , the
relevant snippet here looks like:

	ttk::style configure "." \
	    -borderwidth 	1 \
	    -background 	$colors(-frame) \
	    -foreground 	black \
	    -troughcolor 	$colors(-darker) \
	    -font 		TkDefaultFont \
	(...)

This may be overridden then by corresponding snippets in any theme
definition.

Now since there seems to be no font defined for notebook it
apparently uses TkDefaultFont, so one method to change it might be to
change TkDefaultFont with something like

   somewidget.tk.call('font', 'configure', 'TkDefaultFont', '-size', '28')

Of course this changes the font for every other widget that uses
TkDefaulFont either (which might be intentional when using themes otoh).

If only the font on the ttk notebook tabs should be changed, one has to
define a new font for the TNotebook.Tab resource, as in

    >>> s=ttk.Style()
    >>> s.configure('TNotebook.Tab')
    {'padding': '4 2', 'background': '#c3c3c3'}
    >>> s.configure('TNotebook.Tab', font=('courier', 18))
or
    >>> s.configure('TNotebook.Tab', font='TkTextFont')
    >>> s.configure('TNotebook.Tab')
    {'padding': '4 2', 'font': 'TkTextFont', 'background': '#c3c3c3'}

This works, I am not sure if this is documented anywhere, though.
This does also not seem to be coherent with other ttk widgets, in the ttk
combobox for example the Listbox subwidget seems to share its defaults
with any other listbox widget. Maybe this is because they consider the
notebook tabs as something special, while e.g. the combobox is composed
from "standard" widgets.

To query the default notebook tab font one can do:

    >>> s=ttk.Style()
    >>> s.configure('TNotebook.Tab')
    {'padding': '4 2', 'background': '#c3c3c3'}
    >>> s.lookup('TNotebook.Tab', 'font')
    'TkDefaultFont'
    >>> 

> I've used all the methods of ttk.Style that one uses to explore
> the structure of a theme: .theme_names(), .element_names(),
> .element_options(), .lookup(), .configure(), and .layout().
> All this tells me is that a Notebook has only one element,
> named Notebook.client.

I see, it looks somewhat like one just has to *know* about the
'TNotebook.Tab' style, which is anything but obvious when it is not
documented.


> P.S.  I'm well along on the ttk rewrite of our locally written
> Tkinter manual.  The rest of what's left looks pretty
> straightforward, but I won't be happy with this version unless it
> can help people answer simple questions like the one posed here.
> The draft is here if you don't mind big chunks still missing:
> 
>      http://www.nmt.edu/tcc/help/pubs/tkinter85
> 

Great work, I appreciate it really quite a lot!

Regards

Michael


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

A princess should not be afraid -- not with a brave knight to protect her.
		-- McCoy, "Shore Leave", stardate 3025.3


More information about the Tkinter-discuss mailing list