Tkinter Widgets

Posted : admin On 1/25/2022

Summary: in this tutorial, you’ll learn about Tk themed widgets by using the Tkinter.ttk module.

  1. Python Tkinter Widgets
  2. All Tkinter Widgets

Python Tkinter Canvas Tutorial Like other widgets, the canvas is also a widget that is used for multiple uses. Canvas is a drawing widget and it can hold other widgets as well. Using canvas 2D objects can be created like text, circle, rectangle, etc. Using Ttk¶ To start using Ttk, import its module: from tkinter import ttk. To override the basic Tk. Tkinter, being the large and expansive GUI library that it is, offers us a wide range of widgets to take input in Python. One of these many widgets is the Tkinter Text Widget, which can be. Tkinter: creating the widgets and arranging them on the screen. Later we will talk about how to connect the face—the “front panel”—of the application to the logic behind it. A minimal application. Here is a trivial. Program containing only a Quit button: #!/usr/bin/env python. Import Tkinter as tk. Class Application(tk. AwesomeTkinter offers some pretty tkinter widgets These widgets are just a tkinter widgets with custom styles and images.

Introduction to Tk themed widgets

Tkinter has two generations of widgets:

  • The old classic tk widgets. Tkinter introduced them in 1991.
  • The newer themed ttk widgets added in 2007 with Tk 8.5. The newer Tk themed widgets replace many (but not all) classic widgets.

Note that ttk stands for Tk themed. Therefore, Tk themed widgets are the same as ttk widgets

The tkinter.ttk module contains all the new ttk widgets. It’s a good practice to always use themed widgets whenever they’re available.

The following statements import the classic and the new Tk themed widgets:

And the following program illustrates how to create classic and themed labels:

Output:

They look similar. However, their appearances depend on the platform on which the program runs.

Python Tkinter Widgets

Advantages of using Tk themed widgets

By using the Tk themed widgets, you gain the following advantages:

  • Separating the widget’s behavior and appearance – the ttk widgets attempt to separate the code that implements the widgets’ behaviors from their appearance through the styling system.
  • Native look & feel – the ttk widgets have the native look and feel of the platform on which the program runs.
  • Simplify the state-specific widget behaviors – the ttk widgets simplify and generalize the state-specific widget behavior.

All Tkinter Widgets

The ttk widgets

The following ttk widgets replace the Tkinkter widgets with the same names:

Widgets

And the following widgets are new and specific to ttk:

Summary

  • Tkinter has both classic and themed widgets. The Tk themed widgets are also known as ttk widgets.
  • The tkinter.ttk module contains all the ttk widgets.
  • Do use ttk widgets whenever they’re available.

last modified July 6, 2020

In this part of the Tkinter tutorial, we will coversome basic Tkinter widgets. We work with the following widgets:Checkbutton, Label, Scale, andListbox.

Widgets are basic building blocks of a GUI application. Over the years,several widgets became a standard in all toolkits on all OS platforms;for example a button, a check box or a scroll bar. Some of them might have different names.For instance, a check box is called a check button in Tkinter.Tkinter has a small set of widgets which cover basic programming needs.More specialized widgets can be created as custom widgets.

Tkinter Checkbutton

Checkbutton is a widget that has two states: on and off.The on state is visualized by a check mark. (Some themes may have different visuals.)It is used to denote some boolean property.The Checkbutton widget provides a check box with a text label.

check_button.py

In our example, we place a check button on the window. The checkbutton shows or hides the title of the window.

We create an BooleanVar object. It is a value holder for Booleanvalues for widgets in Tkinter.

An instance of the Checkbutton is created. The value holderis connected to the widget via the variable parameter. Whenwe click on the check button, the onClick() method is called.This is done with the command parameter.

Initially, the title is shown in the titlebar. So at the start, we make itchecked with the select() method.

Inside the onClick() method, we display or hide the title based onthe value from the self.var variable.

Tkinter Label

The Label widget is used to display text or images.No user interaction is available.

Our example shows an image on the window.

By default, the Label widget can display only a limitedset of image types. To display a JPG image, we must use the PIL, PythonImaging Library module. Learn more about PIL with Pillow tutorial.

We create an Image from the image file in thecurrent working directory.

We create a photo image from the image.

The photoimage is given to the image parameter of thelabel widget.

In order not to be garbage collected, the image reference must bestored.

Widgets

We make the size of the window to exactly fit the image size.

Tkinter Scale

Scale is a widget that lets the user graphically selecta value by sliding a knob within a bounded interval. Our example willshow a selected number in a label widget.

We have two widgets in the above script: a scale and a label.A value from the scale widget is shown in the label widget.

A Scale widget is created. We provide the lower and upperbounds. The from is a regular Python keyword that is why there is anunderscore after the first parameter. When we move the knob of the scale,the onScale() method is called.

An integer value holder and label widget are created. Value from the holderis shown in the label widget.

TkinterTkinter Widgets

The onScale method receives a currently selected value from thescale widget as a parameter. The value is first converted to a floatand then to integer. Finally, the value is set to the value holder of thelabel widget.

Tkinter Listbox

Listbox is a widget that displays a list of objects. It allows the userto select one or more items.

listbox.py

In our example, we show a list of actresses in the Listbox.The currently selected actress is displayed in a label widget.

This is a list of actresses to be shown in the listbox.

We create an instance of the Listbox and insert allthe items from the above mentioned list.

Tkinter Widgets

When we select an item in the listbox, the <<ListboxSelect>>event is generated. We bind the onSelect() method to this event.

A label and its value holder is created. In this label we will displaythe currently selected item.

We get the sender of the event. It is our listbox widget.

We find out the index of the selected item using thecurselection method.

The actual value is retrieved with the get() method,which takes the index of the item.

Finally, the label is updated.

In this part of the Tkinter tutorial, we have presented several Tkinter widgets.