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

Asked

Viewed 144 times

7

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

For those who don’t know what the wish, 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, with the saying Click Me which, when clicked, executes the TCL command to print on the console Hello World.

Then we sent for the widget so-called .submit in the pack of screen things.

See working:

wish hello

So far, I was able to do a "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 to change the click command to

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

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

Erro, window name "submit" already exists in parent

Error, window name "Submit" already exists in Parent

which in free translation would be:

Error, window name "Submit" already exists in parent

I mean, just call the button .submit tries to create again a button called .submit

I managed to put a label on the screen with a slight variation:

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

label de 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 I should remove and reinsert the component with the changes?
  • 1

    Which test do you want to change? If you just change the text of the button, you can use the command configure with the property -text. Example: { .submit configure -text "Novo Texto" }

  • @Gomiero didn’t even know he could do the .submit configure. That comment of yours would be a good enough answer for me. Waiting for you to turn the comment into a response so you can accept and share the knowledge

2 answers

4

You can change the text (or any other property) of widget, with the command configure, indicating the property that will 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, by clicking the button .submit, the properties -text (button text) and -fg (text color) is changed to "New Text" and firebrick, respectively.

Reference (in English): Widget - Configure

  • Excellent answer! And thank you so much for the reference

  • @Jeffersonquesado You’re welcome!! :)

1


The button widget has a "textvariable" property just for this. Assign a variable any "v" by the textvariable, which will receive the new text, and every time you change it the text of the button will change as well.

button .b -textvariable v -text "Click-me" -command {set v "Hello Word!"}
pack .b
  • This alternative is much more direct to the point than the previous one. Not that the previous one didn’t work, but yours looks much more idiomatic and using the capabilities of TCL/TK than configure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.