Tcl/Tk Tutorial: My First Splash Screen

Let’s say you would like to build a splash screen like the one below, which lists important phone numbers and email addresses at your site. You can do this using two types of Tk widgets: labels and buttons. We will do this first using the application “wish”, then we will write a script which will perform the same functions, and finally we will incorporate our script into our ANSYS environment.

Splash Screen

Instead of compiling your code, Tcl/Tk uses wish as a Tcl/Tk interpreter that takes in Tcl/Tk scripts and creates the GUI widgets. We will demonstrate this by typing commands directly into the wish console.

If you are using Windows, you can start the “wish” application by choosing Programs | Tcl | Wish from the Start Menu. If you are using UNIX, and if the Tcl/bin directory is in your environment path, simply type wish at the command prompt.

You will see two windows. One is an input window for entering commands, the other is a root graphics window that will contain any widgets that we create. In the input window, issue the following command:
wm title . "ANSYS Support"

This basically says, “In the window, create the title, use the root window as the destination and use the string ‘ANSYS Support’.” Notice the title of your root graphics window now displays “ANSYS Support” instead of “wish83”. In this case, the “.” in the previous command is the name of our root graphics window. All subsets of this window will be preceded by a “.”. Next, we’ll display some useful phone numbers. Issue the following command:
label .name1 -text "John Doe, 555-123-4567, john.doe@mycompany.com"

This creates the label called “name1” in our root window with the text given above in the double-quoted string. Nothing will appear in our graphics window until we tell the interpreter how we would like to display it.
pack .name1 -side top -padx 10 -pady 10

We’ve just told the graphics manager that the label “.name1” will be placed from the top of the main window. In addition, we’ve specified that we want 10 pixels of space to separate this label and the rest of the window. Also, in this particular case, the “-side top” was actually redundant because by default all widgets are packed from the top. We can create as many labels as we want and pack them as well. For example, type in the following to add the other two labels and then pack them in the window:
label .name2 -text "Jane Doe, 555-987-6543, jane.doe@mycompany.com"
label .name3 -text "Someone Else, 555-000-0000, someone.else@mycompany.com"
pack .name2 .name3 -side top -padx 10 -pady 10

Next, we’ll want to create a button so that the user can dismiss this dialog after they read the information.
button .btn -width 15 -text "OK" -command "destroy ."
pack .btn -side bottom -pady 20

We’ve now created and placed a button with the label OK at the bottom of the window. Don’t press this button yet! Upon pressing this button, the application will issue the destroy . command to remove the root graphics window and any children (i.e., our labels and button). When you are ready, press the OK button to exit out of the wish environment. Next, we’ll create a script which will issue all of these commands automatically. Simply copy the contents below into a file called “splash.tcl”.
#!/bin/sh
# the next line restarts using wish
\ exec wish "$0" "$@"
wm withdraw . destroy .splash
set t [toplevel .splash]
wm title $t "ANSYS Support"
label $t.name1 -text "John Doe, 555-123-4567, john.doe@mycompany.com"
pack $t.name1 -side top -padx 10 -pady 10
label $t.name2 -text "Jane Doe, 555-987-6543, jane.doe@mycompany.com"
label $t.name3 -text "Someone Else, 555-000-0000,someone.else@mycompany.com"
pack $t.name2 $t.name3 -side top -padx 10 -pady 10
button $t.btn -width 15 -text "OK" -command "destroy $t"
pack $t.btn -side bottom -pady 20

The first three lines will tell your operating system to invoke the wish interpreter, and all of the following lines will be issued as commands to the wish program. On a Windows machine, assuming Tcl/Tk has been installed properly, this is probably not necessary since all files with a “.tcl” extension are assumed to be Tcl/Tk scripts. Unix users will also have to add execute privileges to this file. Run the script either by double-clicking (for Windows users), or by typing the command splash.tcl. You should see a splash screen identical to the one we created earlier.

Notice that we’ve added a few changes from our previous wish execution. Rather than use the main graphics window as the container for all of our widgets, we have withdrawn the main window so that the window manager will “sort of forget about it”, and created a new top-level window called “.splash”. A variable t has been set to “.splash” so that for each instance of “$t.window”, the window will be a child of “.splash”. The destroy .splash command is issued in the beginning as a safeguard, in case a window named “.splash” already exists (i.e., in the instance that the user runs the script twice without exiting).

Now finally, let’s incorporate this into ANSYS. Copy the file “splash.tcl” into whatever directory is your ANSYS working directory and start up an ANSYS interactive session. In the input window type:
~eui, 'source splash.tcl'

You should now see your splash screen. To add your splash screen to your toolbar type the following:
*ABBR, SUPPORT, ~eui,'source splash.tcl'

Now each time you press the SUPPORT button on your toolbar, your splash screen will show up. If you want this splash screen available to you every time you start ANSYS, simply locate the file “start.ans” in your ANSYS56/DOCU directory, copy it to your working directory and add the above command to the end of the file.

Lastly, let’s say you replaced “Jane Doe” and others with actual people and useful phone numbers, and you would like to make this available to all users. Copy the “splash.tcl” file to the ANSYS56/DOCU directory. Edit the “start56.ans” file in the ANSYS56/DOCU directory (making sure to back it up first) and add the following line:
*ABBR, SUPPORT, ~eui,'source [file join C:/ ANSYS56 DOCU splash.tcl]'

In this case you will probably replace C:/ ANSYS56 DOCU with the directory location of wherever ANSYS is located on your machine.

Congratulations on successfully creating your first Tcl/Tk GUI for ANSYS!

Products & Sales
Engineering Services
Rapid Manufacturing
Product Development
Support & Training
About PADT