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

Trouble changing background of ttk::entry widget

775 views
Skip to first unread message

Casey

unread,
Mar 16, 2011, 11:17:55 PM3/16/11
to
Does the vista theme, support changing the background color?

I'm asking because i'm using this piece of code

ttk::style map TEntry -fieldbackground [list focus yellow]

which doesn't seem to be working.

Pat Thoyts

unread,
Mar 17, 2011, 8:02:24 PM3/17/11
to
Casey <casey....@gmail.com> writes:

The XP theme field element is drawn by the native XP themeing engine so
you don't get to pick and choose - the user gets what she expects from
the theme she has chosen for Windows (be it blue, green, silver or Zune)

To override this you need to replace the field element with one that you
can control more directly. You can do this by cloning the corresponding
element from one of the other themes (default will work) and configuring
it appropriately. The downside in this case is that the field is
handling the border style for the entry widget so you will get control
of the background but you loose control of the border. Maybe you can
work around that -- you can use the vsapi engine to create any XP
theming element you want and try to wrap that around the new background.

Anyway - an example:

# Sample of an overridden Entry widget which has an alternative
# background element that may be set to a given colour.
# Note that the colour is part of the style - not part of the widget data
# so all Plain.Entry widgets will use the same background colours.
#

package require Tk 8.5

proc PlainEntryInit {} {
catch {
# Import the 'default' theme field element.
ttk::style element create plain.field from default
}

# Create a new style using the imported element.
ttk::style layout Plain.Entry {
Plain.Entry.plain.field -sticky nswe -border 1 -children {
Plain.Entry.padding -sticky nswe -children {
Plain.Entry.textarea -sticky nswe
}
}
}

# Configure the colour and padding for our new style.
ttk::style configure Plain.Entry {*}[ttk::style configure TEntry] \
-fieldbackground LightSteelBlue -cursor hand2
ttk::style map Plain.Entry {*}[ttk::style map TEntry] \
-fieldbackground {!focus SteelBlue}

# Re-do if the user changes theme.
if {[lsearch -exact [bind . <<ThemeChanged>>] PlainEntryInit] == -1} {
bind . <<ThemeChanged>> +PlainEntryInit
}
}

# Create a dialog to demonstrate the plain entry widget style
proc Test {dlg} {
variable e "Plain.Entry widget"
variable e2 "normal widget"
wm title $dlg "Plain entry test"
ttk::entry $dlg.e -style Plain.Entry -textvariable ::e
ttk::entry $dlg.e2 -textvariable ::e2
grid $dlg.e -pady 2 -padx 2
grid $dlg.e2 -pady 2 -padx 2
bind $dlg <Control-F2> {console show}
return $dlg
}

if {!$tcl_interactive} {
PlainEntryInit
if {![winfo exists .plainentry]} {
wm withdraw .
set dlg [Test [toplevel .plainentry -class Dialog]]
tkwait window $dlg
exit
}
}
--
Pat Thoyts http://www.patthoyts.tk/
PGP fingerprint 2C 6E 98 07 2C 59 C8 97 10 CE 11 E6 04 E0 B9 DD

0 new messages