Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the color of the BackGround of a selection

Status
Not open for further replies.

erixire

Technical User
Jun 4, 2002
72
CA
Hi,

Is it possible to change the background color of the selection in a Text Widget? By default, when you double-click on a word, it is highlighted in Black and the foreground color is white. But I want to put the background color has another color. Can somebody explain how to this please?

Thanks.
 
From the manual:

The sel tag is automatically defined when a text widget is created, and it may not be deleted with the “pathName tag delete” widget command. Furthermore, the selectBackground, selectBorderWidth, and selectForeground options for the text widget are tied to the -background, -borderwidth, and -foreground options for the sel tag: changes in either will automatically be reflected in the other.

You can try:
Code:
pack [text .t]
.t config -selectbackground red

Good luck

ulis
 
What you're asking for is fairly simple, but let me give you a bit of context to understand what's going on. You might discover some other really nifty things you can do with a text widget in the process. :)

There are a variety of options that you can configure on the widget itself that provide default appearance settings for your text. For example

Code:
text .t -foreground blue     -font {courier 10 bold}
pack .t -expand yes -fill both

Now any text you enter will show up blue in boldface courier.

However, the text widget also allows you to change the appearance of individual pieces of text. You do so by applying tags to text, and then configuring the options of that particular tag. Here's an example:

Code:
.t tag configure highlight     -foreground red -font {courier 12 bold}

# Insert the text "Look out!\n" and give it
# the tag "highlight".

.t insert end "Look out!\n" highlight

# Insert another line, mixing regular text
# (no tag, indicated by {}) and text with
# the "highlight" tag.

.t insert end "Normal text with an " {}     "alert" highlight " in it.\n" {}

The reason I described all this is that the text widget has a pre-defined tag called "sel". It automatically applies the "sel" tag to whatever characters are currently selected. So, to change the appearance of the selection, just change the configuration of the "sel" tag options. For example:

Code:
# Selected text will now appear in
# black against a yellow background.

.t tag configure sel -foreground black     -background yellow

Tags also allow you to make text sensitive to events. You can actually create bindings to tags and respond in any way that you like. This is how people have used the text widget to create simple hypertext browsers with links. For example, run this command (assuming that you've created the "highlight" tag mentioned above) and then click on any highlighted text:

Code:
.t tag bind highlight <ButtonPress-1> {
  .t insert end     &quot;You clicked some highlighted text.\n&quot;
}

The text reference page has tons more information on the various tag configuration options and how you can apply and use tags. Have fun! - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top