Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ttk style question

281 views
Skip to first unread message

Zoltan Kocsi

unread,
Jul 3, 2015, 10:59:54 AM7/3/15
to
I'm looking for some help from someone who actually understands how
styles work in ttk:: widgets.

If I have a widget, say ttk::labelframe with the instance name
of .lf then the command

winfo class .lf

gives me its class, TLabelframe. According to The Book (Oustrhout's Tcl
book, second ed) that's the style the widget uses. Cool, if I want to
change some aspect of the widget, I need to modify that style and I'm
done. More precisely, I need to create a new style that overrides this
or that in the base style and tell the widget that it should use the
style I created rather than the default.

Sounds easy enough, so let's do a little introspection. First we need
to know what elements a labelframe is constructed of. Fire up wish, and
take a look; according to the book we need to use the class name again:

% ttk::style layout TLabelframe
Labelframe.border -sticky nswe

Oops. So where's the label?

Because, as it turns out, there is a Tlabelframe.label element that has
a bunch of options controlling the appearance of the label, but how can
I learn what elements a particular widget is composed of?

One would hope that 'ttk::style element names' would be to the rescue
as the man page says:

"Returns the list of elements defined in the current theme."

So, if I get the list, I just look for labelframe or label or something
so I get a hint. Let's try it:

% ttk::style element names
Combobox.field downarrow Menubutton.indicator border leftarrow
Radiobutton.indicator vgrip thumb uparrow trough rightarrow hgrip
slider field pbar bar Checkbutton.indicator client tab

Whoa. No element with the word 'label' in its name.

I'm really confused and would much appreciate any help.

Given an arbitrary ttk widget, how can I figure out what it is composed
of (i.e. its configurable sub-elements) and how should I define a style
to change any particular visual aspect of that widget?

I mean, apart from reading the source code for each and every theme -
there must be an easier way, at least I hope there is. After all, the
whole themed widget set was created so that visual appearances are easy
to control, no need to fine-tune every aspect of every widget
individually.

I would be obliged if someone could point me to a document that clearly
explains the hierarchy of objects and properties inside ttk, how
layouts, styles, themes, elements and options are organised, interact
and generally, how the whole ttk widget set works and what tools (i.e.
Tcl commands) are available to actually explore the object hierarchy.

Thanks in advance,

Zoltan
--
Zoltán Kócsi
Bendor Research Pty. Ltd.

Olivier

unread,
Jul 4, 2015, 6:06:13 AM7/4/15
to

> I would be obliged if someone could point me to a document that clearly
> explains the hierarchy of objects and properties inside ttk, how
> layouts, styles, themes, elements and options are organised, interact
> and generally, how the whole ttk widget set works and what tools (i.e.
> Tcl commands) are available to actually explore the object hierarchy.
>

Hi,

There is really an unfinished documentation concerning ttk::widget, it's true, and looks like handcraft than coding !

If you think an element exist try a command that seems logical until it gives something, for instance :

ttk::style element options TLabelframe.label

will return :

-compound -space -text -font -foreground -underline -width -anchor -justify -wraplength -embossed -image -stipple -background

The clearest and most complete documentation I know is on this page : http://www.tkdocs.com/tutorial/styles.html

So, I've created some small scripts that can give me information for ttk::widget. The difficulty is that unexisting possibilities don't return error, for instance :

ttk::style element options Labelframe.border --> returns "" ( but no error )
ttk::style element options TLabelframe.border --> return "-relief"

on the other hand :

ttk::style element options TLabelframe.label
and
ttk::style element options Labelframe.label

return the same thing !

Using brute force, I simply try each possibility given by "element names" "layout" or "element options".

As an exemple, here is an ugly example for labelframes :-/ :

#------------------------------------------------------

et output [open get_themed_labelframe.txt w]

ttk::style theme use xpnative
# ttk::style theme use winnative
# ttk::style theme use alt
# ttk::style theme use aqua
# ttk::style theme use clam
# ttk::style theme use classic
# ttk::style theme use vista

font create TkfabLabelFrameFont {*}[font configure TkDefaultFont] -weight normal -size 8
#font configure TkfabLabelFrameFont -weight normal -size 8
puts $output "font configure TkfabLabelFrameFont"
puts $output "[font configure TkfabLabelFrameFont]"
puts $output "\n"

puts $output "ttk::style theme names"
puts $output "[ttk::style theme names]"
puts $output "\n"

puts $output "ttk::style theme use"
puts $output "[ttk::style theme use]"
puts $output "\n"

puts $output "ttk::style element names"
puts $output "[ttk::style element names]"
puts $output "\n \n"



# Create a dialog to demonstrate the new entry widget style
set dlg [toplevel .test_labelframe ]


wm title $dlg "ttk::labelframe test"
ttk::labelframe $dlg.lf -text test

set usedStyle_ifnotdefault [$dlg.lf cget -style]
set usedStyle [winfo class $dlg.lf]
puts $output "usedStyle of $dlg.lf : $usedStyle"
puts $output "usedStyle_ifnotdefault of $dlg.lf : $usedStyle_ifnotdefault"
puts $output "\n"

puts $output "LAYOUT LAYOUT LAYOUT"

puts $output "ttk::style layout TLabelframe"
puts $output "[ttk::style layout TLabelframe]"
puts $output "\n "

puts $output "OPTIONS OPTIONS OPTIONS"

puts $output "ttk::style element options Labelframe.label"
puts $output "[ttk::style element options Labelframe.label]"
puts $output "ttk::style element options TLabelframe.label"
puts $output [ttk::style element options TLabelframe.label]
puts $output "\n "

puts $output "ttk::style element options Labelframe.border"
puts $output "[ttk::style element options Labelframe.border]"
puts $output "ttk::style element options TLabelframe.border"
puts $output [ttk::style element options TLabelframe.border]
puts $output "\n "

puts $output "LOOKUP LOOKUP LOOKUP"

puts $output "ttk::style lookup TLabelframe.border -relief"
puts $output "[ttk::style lookup TLabelframe.border -relief]"
puts $output "ttk::style lookup TTLabelframe.border -relief"
puts $output "[ttk::style lookup TTLabelframe.border -relief]"
puts $output "\n "

puts $output "ttk::style lookup TLabelframe.label -relief"
puts $output "[ttk::style lookup TLabelframe.label -relief]"
puts $output "ttk::style lookup TTLabelframe.label -relief"
puts $output "[ttk::style lookup TTLabelframe.label -relief]"
puts $output "\n "

puts $output "ttk::style lookup TLabelframe.Label -relief"
puts $output "[ttk::style lookup TLabelframe.Label -relief]"
puts $output "ttk::style lookup TTLabelframe.Label -relief"
puts $output "[ttk::style lookup TTLabelframe.Label -relief]"
puts $output "\n "


close $output
#wm withdraw .
#tkwait window $dlg
exit

------------------------------------------

Olivier

Zoltan Kocsi

unread,
Jul 4, 2015, 9:16:15 AM7/4/15
to
Oliver,

Thanks for the help!

It's quite sad that ttk is so badly documented. It's quite old (I think
the original paper was in 2002 or 2004 or something). Since it's not
documented, you can't really trust that in the next revision everything
will be called the same.

The whole idea of creating your own styles and use them for widget
groups and everything just happens in the background without you
needing to know and do the nitty-gritty is absolutely brilliant.

Except that apparently you can't do it properly.
You have do a bit of trial-and-error, look at the source, hardcode
internal component and element names that you get by peeking inside.
That is, it's a mess with absolutely no guarantee that it would work
with a different theme on the same platform, let alone a different
version of Tk on a different platform :-(

Thanks again,

Mark Garvey

unread,
Jul 17, 2015, 6:50:53 AM7/17/15
to
Before anyone misses the point, I think it is all incredibly flexible
and powerful. Sure you can't do everything you may want to all of the
time, and you may have to play around a bit first, but there is usually
a nifty workaround for anything which might be missing.

The mind boggles at the possibilities. I completely restyled the toolbar
of an application using ttk:checkbuttons, (ttk:radiobuttons) and
ttk:buttons using styles with image elements with gradients.
All buttons look the same even though they work in different ways and
all fit into the new toolbar based on ttk:frame also using a image
element gradient. To top it off I followed it with a canvas also using a
(programmed) gradient background with transparent images on top!

It all looks like it all stems from Windows 8 and I'm pretty sure it
will work anywhere with 8.5.13 upwards.

Making a ttk:checkbutton look like a ttk:button really was an eye-opener
for me. Play with it!

mse...@gmail.com

unread,
Jul 20, 2015, 4:31:15 AM7/20/15
to
This sounds great, is is possible that you could post on the wiki some examples of this code, just to give others a few more pointers.

Martyn

Mark Garvey

unread,
Jul 20, 2015, 1:29:32 PM7/20/15
to
Try this on the wiki now in:

http://wiki.tcl.tk/41621

mse...@gmail.com

unread,
Jul 21, 2015, 5:06:17 AM7/21/15
to
On Monday, 20 July 2015 19:29:32 UTC+2, Mark Garvey wrote:
> Try this on the wiki now in:
>
> http://wiki.tcl.tk/41621

Thanks that is great and super quick

Martyn

Ralf Fassel

unread,
Jul 21, 2015, 6:19:05 AM7/21/15
to
* Mark Garvey <mark....@hotmail.de>
| http://wiki.tcl.tk/41621

Very neat!

R'

Zoltan Kocsi

unread,
Jul 24, 2015, 8:48:53 AM7/24/15
to
On Fri, 17 Jul 2015 12:50:49 +0200
Mark Garvey <mark....@hotmail.de> wrote:

> [...]
> The mind boggles at the possibilities. I completely restyled the
> toolbar of an application using ttk:checkbuttons, (ttk:radiobuttons)
>[..]
> Making a ttk:checkbutton look like a ttk:button really was an
> eye-opener for me. Play with it!

Yes, it's very nice. However, all I wanted to do was changing *one*
simple feature of *one* widget, one of the simplest. I didn't want to
do a new style, I just wanted a slightly larger font for the
ttk::labelframe, that's all. The app I need to build should work on
Windows, Mac and Linux and should conform the styles of the native
interfaces, so no new styles - but a slightly larger font helps
visibility.

I did not question the capabilities of Ttk's style system. I questioned
its documentation (or lack thereof) and lack of consistency in naming
the elements.

It is powerful. Very powerful. Especially if you go through the source
so that you know which widget has what component and it has what
properties under what names.

Mark Garvey

unread,
Jul 31, 2015, 7:21:09 AM7/31/15
to
Yes sorry about that. I quite understand your point.
When the answers dried up, I wanted to make sure nobody decided there
was never any need in trying to do anything special with it. It could
use a good book and a little more/better introspection code to make it
really sing.
0 new messages