Creating a widget

The basic method for creating a widget is the same regardless of the widget being created. The differences are in the properties of the widget and if any action can be assigned to it. In the following chapters, we will investigate specific widgets and their properties in depth, but before we reach this point, I would like to introduce you to the button widget and some basic features.

How to do it…

In the following example, we will create a button widget with some basic properties and a single action. Enter the following commands:


1 % button .b -text "Exit" -command exit
2 % pack .b

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 the word Exit and it will execute the exit command when clicked. Now click on the button that you have created. You will see that you have exited the wish program by activating the exit command.

There's more…

As I stated in the previous section, the basic method for creating a widget is the same regardless of which widget is being created. To illustrate this, we will now create a simple label containing some text. To accomplish this, enter the following commands:


1 % label .l -text "Label"
2 % pack .l

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

There's more…

In the button example, we created a widget by specifying the label widget and set its text property to contain the string "Label".

Since version 8.5, Tk has included themed widgets. They are designed to mimic the appearance of the native operating system and provide a consistent look and feel to our applications.

These widgets are accessed by invoking the ttk:: namespace. For example, where we created our button with the button command, we would access the themed widgets via the ttk::button command, as opposed to the button command.

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

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