Reputation: 189
So I have this type of button with this options.
button .but1 -text "Sample" -width 10 -height 2 -background $btnColor -activeforeground $actForegrd -state normal -activebackground $activbtnColor -relief flat -highlightthickness 0
is there a way to make a global variable with those options for button, I know about style configure, but in order to use style, I need to change my buttons to ttk::button and those don't have the same options as the regular button.
I try putting all the options in a string and pass it when creating a button but it doesn't work example:
set style "-width 10 -height 2 -background $btnColor -activeforeground $actForegrd -state normal -activebackground $activbtnColor -relief flat -highlightthickness 0"
button .but1 $style
Upvotes: 0
Views: 477
Reputation: 137767
There's two basic possibilities.
What you tried is almost right. Try this:
set style "-width 10 -height 2 -background $btnColor -activeforeground $actForegrd -state normal -activebackground $activbtnColor -relief flat -highlightthickness 0"
button .but1 {*}$style
The {*}
(which entered Tcl in 8.5) makes what follows it be expanded to many arguments.
Instead, you can put all those values into the option database using the option
command:
option add *.Button.width 10
option add *.Button.height 2
option add *.Button.background $btnColor
option add *.Button.activeForeground $actForegrd
option add *.Button.state normal
option add *.Button.activeBackground $activbtnColor
option add *.Button.relief flat
option add *.Button.highlightThickness 10
## You can also pull the above from a separate file with:
# option readfile ~/thefile.opts
button .but1
Using the option database right and well is tricky, but it lets you set the defaults for many widgets at once (or a single one, or just widgets that are children of a particular window, or of a class of windows, or …) You'd be truly splitting the style from the code.
I wouldn't normally have the state
set by this mechanism though, as that's more of a semantic option; if a button is going to be disabled or not really does matter to code (just as the callback to run when that happens is another one I'd not set via options).
It should be noted that the Ttk style/theme mechanism largely supersedes this, as it allows for much more substantial modifications and cleaner runtime switching between them. But there are a number of effects that are quite a bit harder to do that way; we still maintain the classic button
, etc. for a reason.
Upvotes: 2