Tcl/Tk tutorial: Hello world

This tutorial helps you get started with Tcl/Tk. It explains what you need to write your first Tcl/Tk program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a Tcl/Tk program before this is the place to start. We have created this article, so you can get started with Tcl/Tk, and move on to our sms examples. If you are already familiar with Tcl/Tk, you can jump directly to one of the following SMS projects.

Td/Tk sms examples:

Td/Tk send sms with the HTTP rest api (code sample)
Td/Tk send multiple sms with the HTTP rest api (code sample)
Td/Tk schedule sms with the HTTP rest api (code sample)
Tcl/Tk receive sms with the HTTP rest api (code sample)
Tcl/Tk delete sms with the HTTP rest api (code sample)
Download the latest Tcl/Tk sms api library from Github

What is Tcl/Tk

Tcl/Tk is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is a Tcl/Tk 'hello world' program

The Tcl/Tk hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Tcl / Tk

To write your first program in Tcl / Tk:

  1. Check prerequisites
  2. Setup Tcl / Tk for Ubuntu
  3. Open Terminal
  4. Create a new .tcl file
  5. Write Hello World program in Tcl / Tk
  6. Run your Tcl / Tk Hello World program
  7. Check the output
  8. Fix errors

Prerequisites

Here is what you need to get started. To write your first computer program in Tcl/Tk, you need a computer with Ubuntu, the Tcl/Tk language and example code presented below.

  • Ubuntu computer
  • Tcl/Tk language
  • Ozeki Hello World example project

Download and install the Tcl/Tk language

To download Tcl/Tk for Ubuntu, you have to open the terminal and type in the following command:

sudo apt install tcl
	

Code 1 - Command to install Tcl/Tk for Ubuntu

Video 1 - The installation process Tcl/Tk for Ubuntu

How to install Tcl/Tk on Ubuntu

To install Tcl/Tk on Ubuntu you first need to open the terminal. Type sudo apt install tlc and hit enter (Figure 1). This will begin the instlalation process. When it is finished, Tcl/Tk is ready to be used for coding on your device.

how to install tcl tk on ubuntu
Figure 1 - How to install Tcl/Tk on Ubuntu

Write your 'hello world' program in Tcl/Tk language

To write your 'hello world' program in Tcl/Tk, you have to create a file with a .tcl extension.
In order to do that you can use the terminal by entering the following command:

Notice: It is recomended to cd into a known directory such as the Desktop.
You can use this command to do that: cd Desktop

touch main.tcl
	

Code 2 - Command to create a new .tcl file in Ubuntu

puts "Hello world!";
	

Code 3 - Your first 'hello world' program in Tcl/Tk

Video 2 - How to write a 'hello world' program in Tcl/Tk

How to run your Tcl/Tk program on Ubuntu

To run our Tcl/Tk program in Ubuntu we will use the Terminal.
To run the .tcl file we will use the following code:

tclsh main.tcl
	

Code 4 - Command to run your Tcl/Tk code int the terminal

Video 3 - How to run your Tcl/Tk code int the terminal

How to run your Tcl/Tk code

To run the Tcl/Tk program in Ubuntu, you need to open the terminal again. Simply type tclsh main.tcl and hit enter. This will execute the code. You can see that the "Hello World!" message has appeared. This is the output of the code.

Figure 2 - Run the Tcl/Tk Hello word program

What happens if you make a mistake in Tcl/Tk

If you make a mistake in your code and you run it, there will be an error report in your output, which makes it easier for you to recognize the nature of the error, and it will also tell you in which line did the error occur.

In the following video I will make an error in the code by deleting one of the quote marks.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in Tcl/Tk and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in Tcl/Tk?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Video 4 - What happens if you make a mistake in your code

Error report in Tcl/Tk

No we are going to take a look at what happens when the program encounters an error while running. We intentionally removed a quotation mark at the end of our command. This leads to a syntax error. When you run the program this way, it will not display hello world. An error message appears instead, which tells you what went wrong and where it happened (Figure 3). This is incredibly useful, since it tells you that a quotation mark is missing. These error messages can help you fix mistakes.

Figure 3 - Error report in Tcl/Tk

Summary

This tutorial helps you to get started with Tcl/Tk by explaining the base steps from the beginning, such as what prerequisites you need to install and how to create your first Tcl/Tk program. This knowledge you obtained gives you the ability to use the Ozeki PHP SMS solutions on your Tcl/Tk application to reach a better-performing messaging system. Using Tcl/Tk SMS API is beneficial because it generates a smoother workflow by allowing you to personalize your messaging system based on the needs of your working community.

You can find more information about Tcl/Tk solutions and the Ozeki SMS Gateway, so do not stop here. I recommend reading the tutorial about how to send SMS from Tcl/Tk.

The best time to download the Ozeki SMS Gateway is now, do it!

More information