Version 2 of Problems using Tile/Ttk

Updated 2009-05-12 14:07:08 by MG

MG has started this page to list some problems he's been having trying to switch to Tile/Ttk widgets in an app using Tk 8.5. All of the development I'm doing is on Windows XP with a "Classic" Windows look, and I'm hoping someone might be able to help me out with a few of the problems...

My latest hurdle is trying to get coloured buttons. In classic Tk, this is a case of doing:

  button .b -background red

In Tile, I've tried things along the lines of

  ttk::button .b
  1 % ttk::style configure TButton -background red
  2 % ttk::style configure TButton.Button -background red
  3 % ttk::style configure TButton.Button.label -background red

... and so on. Number 1 there has some effect, but not the desired one - I just get a red border around the outside of the button. 2 and 3 (and all kinds of variations, based on the layout elements returned by ttk::style layout TButton) have no effect.

But, if I do #1 and then switch to a different theme from my default (xpnative):

  ttk::setTheme clam

The button changes to a Clam button - and it's red! Now, someone has shown me in the past how I can hack bits out of the clamTheme.tcl file before to basically steal their button for my theme, but that's not hugely helpful - the whole reason I'm trying to switch to Tile is for properly native widgets, and (while the Clam button is a very attractive shape), it's definately not native. (Ironically, on Windows with the Classic look, my red Tk button looks totally native, but I'd like to do this so it works cross-platform, and even on my Win XP system with the "Classic" look turned off, regular Tk buttons stick out.)

So, my question: is there a way to actually get a native looking button, in the color of my choice, no matter the theme I'm using? Or would I be better just admitting defeat here, and sticking with a regular old Tk button, and hoping noone notices that it's different from all the other buttons in my app? This has been driving me mad on and off for months, so any help someone in the know could offer would be greatly appreciated. Thanks for your time.

Martyn Smith Have you tried looking at http://www.tkdocs.com/tutorial/styles.html which explains exactly what you are trying to do and how to do it. You could also look at the tcl files as part of tile which define the toolbutton style.

   button .b -style Toolbutton

Don't forget changing the TButton style changes ALL your buttons.

MG I have (it's basically my first stop when I'm having issues with Ttk, now), but I couldn't find anything there that actually worked on Windows. The problem I'm having isn't that I can't define a style to change the -background option for my button, it's that the option doesn't actually change the color of the button on Windows, just a border around it. For other themes (or other options, like -foreground) it works just as I'd expect.

For instance, taking one of the examples from that page (colors changed just to make it a bit more visually distinct):

  ttk::style map TButton \ 
        -background [list disabled green  active red] \ 
        -foreground [list disabled blue] \ 
        -relief [list {pressed !disabled} sunken] \ 

  pack [ttk::button .btn -text "Example"]
  .btn state disabled

Then I change the theme to clam:

  ttk::setTheme clam

and repeat the ttk::style map .... This is how the two different buttons look:

http://www.talvo.com/misc/tile-btn-problem.png

What I'm trying to do is get a native looking button like the first, but with a proper green background like the clam button has. There must be something, somewhere, setting the button's color to grey (well, SystemButtonFace), but the -background option is referring to a different element of the widget. (Given the difference between this and other Tk/Ttk widgets' use of -background, and of -border options, I would go so far as to say the wrong element of the widget.)