"Hello, world" with Wish TCL / TK, how to change text in a widget?

6

I'm having trouble understanding how the wish program works, to make a good old "Hello, World".

For those who do not know what wish is, it is a TCL interpreter ready to make graphical interfaces in TK. You program in TCL, create your widgets and position them on the screen.

For example, to make a button that prints Hello World on the console (with shebang and all):

#!/usr/bin/env wish

button .submit -text "Click Me" -command { puts "\nHello World" }
pack .submit

In the first line, we create a button identified as .submit , saying Click Me which, when clicked, executes the TCL command to print on the Hello World console.

Next, we have the widget named .submit in the package of things on the screen.

See working:

Sofar,I'vebeenabletomakea"Hello, World" on the console, but not on the screen. I would like to change the text of a label, or even the button itself, to "Hello, World". I tried changing the click command to

{ puts "\nHello World";  button .submit -text "Hello" }

however ... I am greeted with the following message:

Error,windowname"submit" already exists in parent

  

Error, window name "submit" already exists in parent

that in free translation would be:

  

Error, "submit" window name already exists in parent

That is, simply calling button .submit tries to again a button called .submit

I got a label on the screen with a slight variation:

{ puts "\nHello World";  label .teste -text "Hello"; pack .teste }

But clicking the button again will cause the same error that the component already exists on the screen.

So, my question is:

  • How to change the test of a widget already placed on the screen?
  • Or should I remove and reinsert the component with the changes?
asked by anonymous 23.06.2018 / 02:41

1 answer

4

You can change the text (or any other property) of the widget , with the command configure , indicating the property to be changed and the new value.

Example:

button .submit -text "Click Me" -command { 
     .submit configure -text "Novo Texto"
     .submit configure -fg firebrick
  }

In this example, when you click the .submit button, the -text (button text) and -fg (text color) properties are changed to "New Text" and firebrick respectively.

Reference: Widget - Configure

    
23.06.2018 / 04:11