More Labels

More Labels

It's also possible to change the value of options for a widget programmatically, after the widget has been created. You do this using the label configure command. Thus, you can write:

label .l -text hello
.l configure -bg red

This will behave just like if you had specified the -bg option in the command to create the label. Every Tk widget supports a configure command that knows about the options supported by that type of widget. Here's a more interesting example. In the following Tclet, I'm using two widgets, a label and a button (the blue one, marked click!). When you click on the button, the label's text is changed, using configure:


In this Tclet I set up a counter to count the number of times you clicked. The counter gets incremented every time you click, and the label shows whether you clicked an even or odd number of times. Initially you clicked zero times, so the label shows "even".

You can go wild and change several attributes of the label at the same time, using one configure command. However, restraint is advised, or you'll end up with Tclets only your Mother could love.


In this example, I am changing the text of the label, background color, the color of the text and which character in the label's text is underlined. This is too garish! You can do this, but it doesn't mean you have to.



comments?