Writing to the console

Although rarely used in live programs, the console is a valuable tool during initial development. It allows us to direct debugging information easily, without requiring the overhead of creating dialog boxes (covered later in this book). To allow us to display a console on systems that have no console, or to reveal a hidden console, Tcl provides the console command. The console command accepts four keywords as follows:

Keyword

Interpretation

eval script

Evaluate the script argument as a Tcl script within the console interpreter.

hide

Hide the console.

show

Display the console.

title string

Query OR modify the console title.

The syntax is as follows:

	console keyword argument

How to do it…

In the following example, we will hide the console and then create a set of buttons to display the console and to write a message. Enter the following commands:


1 % button .b -text "Show Console" -command {console show}
2 % button .c -text "Message" -command {puts "My Message"}
3 % pack .b .c
4 % console hide

At this point, your window should look like the following:

How to do it…

How it works…

The first line instructs the wish shell to create a button named b, which is a child of the parent window identified by the '.' character. This button will have a text label containing Show Console and it will execute the console show command when clicked. The second line instructs the wish shell to create a button named c, which is a child of the parent window identified by the '.' character. This button will have a text label containing Message and it will execute the puts command when clicked. Next, we have used the pack command (this command is a windowing geometry manager) to pack our buttons followed by hiding the console.

Now click on the Show Console button. At this point, the console will be visible. Next we will click on the Message button to display the desired text. Using this methodology you can easily direct messages to the console and provide yourself and your end user with information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.212