Tk – Widgets Overview

  • Post author:
  • Post category:Tk
  • Post comments:0 Comments
Tk - Widgets Overview

The basic component of Tk widgets overview is a Tk-based application is called a widget. A component of widgets is also sometimes called a window for widgets overview, since, in Tk, “window” and “widget” are often used interchangeably. Tk is a package that provides a rich set of graphical components for creating graphical applications with Tcl widgets overview.

Tk provides a range of widgets ranging from basic GUI widgets like buttons and menus to data display widgets. The widgets are very configurable as they have default configurations making them easy to use.

Tk applications follow a widget hierarchy where any number of widgets may be placed within another widget, and those widgets within another widget. The main widget in a Tk program is referred to as the root widget and can be created by making a new instance of the TkRoot class.

Creating a Widget

The syntax for creating a widget is given below.

type variableName arguments options

The type here refers to the widget type like button, label, and so on. Arguments can be optional and required based on individual syntax of each widget. The options range from size to formatting of each component.

Widget Naming Convention

Widget uses a structure similar to naming packages. In Tk, the root window is named with a period (.) and an element in window, for example button is named .myButton1. The variable name should start with a lowercase letter, digit, or punctuation mark (except a period). After the first character, other characters may be uppercase or lowercase letters, numbers, or punctuation marks (except periods). It is recommended to use a lowercase letter to start the label.

Color Naming Convention

The colors can be declared using name like red, green, and so on. It can also use hexadecimal representing with #. The number of hexadecimal digits can be 3, 6, 9, or 12.

Dimension Convention

The default unit is pixels and it is used when we specify no dimension. The other dimensions are i for inches, m for millimeters, c for centimeters and p for points.

Common Options

There are so many common options available to all widgets and they are listed below in the following table −

Sr.No.Syntax & Description
1-background colorUsed to set background color for widget.
2-borderwidth widthUsed to draw with border in 3D effects.
3-font fontDescriptorUsed to set font for widget.
4-foreground colorUsed to set foreground color for widget.
5-height numberUsed to set height for widget.
6-highlightbackground colorUsed to set the color rectangle to draw around a widget when the widget does not have input focus.
7-highlightcolor colorUsed to set the color rectangle to draw around a widget when the widget has input focus.
8-padx numberSets the padx for the widget.
9-pady numberSets the pady for the widget.
10-relief conditionSets the 3D relief for this widget. The condition may be raised, sunken, flat, ridge, solid, or groove.
11-text textSets the text for the widget.
12-textvariable varNameVariable associated with the widget. When the text of widget changes, the variable is set with text of widget.
13-width numberSets the width for widget.

A simple example for options is shown below.

#!/usr/bin/wish

grid [label .myLabel -background red -text "Hello World" -relief ridge -borderwidth 3]
   -padx 100 -pady 100

When we run the above program, we will get the following output.

The list of available widgets are categorized below −

Basic widgets

Sr.No.Widget & Description
1LabelWidget for displaying single line of text.
2ButtonWidget that is clickable and triggers an action.
3EntryWidget used to accept a single line of text as input.
4MessageWidget for displaying multiple lines of text.
5TextWidget for displaying and optionally edit multiple lines of text.
6ToplevelWindow with all borders and decorations provided by the Window manager.

Layout Widgets

Sr.No.Widget & Description
1FrameContainer widget to hold other widgets.
2PlaceWidget to hold other widgets in specific place with coordinates of its origin and an exact size.
3PackSimple widget to organize widgets in blocks before placing them in the parent widget.
4GridWidget to nest widgets packing in different directions.

Selection Widgets

Sr.No.Widget & Description
1RadiobuttonWidget that has a set of on/off buttons and labels, one of which may be selected.
2CheckbuttonWidget that has a set of on/off buttons and labels, many of which may be selected..
3MenuWidget that acts as holder for menu items.
4ListboxWidget that displays a list of cells, one or more of which may be selected.

Mega Widgets

Sr.No.Widget & Description
1DialogWidget for displaying dialog boxes.
2SpinboxWidget that allows users to choose numbers.
3ComboboxWidget that combines an entry with a list of choices available to the use.
4NotebookTabbed widget that helps to switch between one of several pages, using an index tab.
5ProgressbarWidget to provide visual feedback to the progress of a long operation like file upload.
6TreeviewWidget to display and allow browsing through a hierarchy of items more in form of tree.
7ScrollbarScrolling widgets without a text or canvas widgets.
8ScaleScale widget to choose a numeric value through sliders.

Other Widgets

Sr.No.Widget & Description
1CanvasDrawing widget for displaying graphics and images..

We will cover each of these widgets in the upcoming chapters.

Next Topic : Click Here

Leave a Reply