This documentation is quite confusing.

Jason Miller jason at milr.com
Tue Jul 30 23:55:30 UTC 2019


On Fri, 26 Jul 2019 20:58:20 +0200, Tom Hassel <trashtalk217 at gmail.com> wrote:
> I've been getting into ltk, since I wanted to do some GUI programming
> again. I'm currently trying some things, one of them being changing the
> color of a button. This should be possible becaus of this bit of
> documentation <http://www.peter-herth.de/ltk/ltkdoc/node18.html>. But it is
> not really specified how I could do it. I've tried with 'configure' and I
> tried in the initialization code and even in the 'pack' function, but it
> does not seem to work. It might be nice if the documentation told me how to
> do this.
> 
> The working code looks like this. How do I change the color of the button?

LTK will use the ttk widgets whenever possible, so you have a ttk::button
which, as you can see from the tkdocs
(http://tcl.tk/man/tcl8.6/TkCmd/ttk_button.htm) does not have a background option.

Instead you will have to create a style for your button that has a black
background.  A first pass might look something like this (I used format-wish
because I could not find any builtin LTK commands for manipulating styles):

  (with-ltk ()
     (let* ((frame (make-instance 'frame))
            (button (make-instance 'button
                                   :master frame
                                   :text "Hello, World"
                                   :command (lambda () (print "Hello, World")))))
       (format-wish "ttk::style configure black.TButton -background black")
       (configure button :style "black.TButton")
       (pack frame)
       (pack button)))

NB: Styles in Tk are in a global namespace with inheritence based upon names,
so "black.TButton" inherits all attributes of "TButton" (the default style for
a button) which in turn inherits from the root style "."

You may notice that the above code causes the button to go back to its default
color if you hover over it.  This is because we only changed the *defualt*
background color, not any of the dynamic background colors.  If you disable the
button, it will similarly return to its default color.  Dynamic attributes are
set with "ttk::style map" and are stored in Tk's equivalent of a plist.  To set
all dynamic colors also to black the following should work:

  (with-ltk ()
    (let* ((frame (make-instance 'frame))
           (button (make-instance 'button
                                  :master frame
                                  :text "Hello, World"
                                  :command (lambda () (print "Hello, World")))))
      (format-wish "ttk::style configure black.TButton -background black")
      (format-wish "ttk::style map black.TButton -background [list active black disabled black readonly black]")
      (configure button :style "black.TButton")
      (pack frame)
      (pack button)))

If you only want to override some of the dynamic background values, you can use
a shorter list.

There's sufficient introspection and documentation in Tk to discover all
possible values on your own, but a good cheatsheet is can be found at
https://wiki.tcl-lang.org/page/Changing+Widget+Colors

HTH,
Jason


More information about the ltk-user mailing list