Summary: in this tutorial, you’ll learn about Tk themed widgets by using the
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
tkwidgets. Tkinter introduced them in 1991.
- The newer themed
ttkwidgets added in 2007 with Tk 8.5. The newer Tk themed widgets replace many (but not all) classic widgets.
ttk stands for Tk themed. Therefore, Tk themed widgets are the same as
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:
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
ttk widgets replace the Tkinkter widgets with the same names:
And the following widgets are new and specific to
- Tkinter has both classic and themed widgets. The Tk themed widgets are also known as
tkinter.ttkmodule contains all the
- Do use
ttkwidgets 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:
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.
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.
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
Initially, the title is shown in the titlebar. So at the start, we make itchecked with the
onClick() method, we display or hide the title based onthe value from 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.
We make the size of the window to exactly fit the image size.
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.
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.
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.
Listbox is a widget that displays a list of objects. It allows the userto select one or more items.
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.
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 the
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.