List of Python TKinter Widgets with Examples

In this lesson we want to learn about List of Python TKinter Widgets with Examples.

 

 

 

What is TKinter ?

Tkinter is a Python library that is used for creating desktop graphical user interfaces (GUIs). It provides a set of widgets (e.g., buttons, labels, text boxes, etc.) that can be used to create windows, dialogs, and other types of graphical user interfaces. Tkinter is built on top of the Tk library, which is a GUI toolkit originally written for Tcl, a programming language that is similar to Python. The Tk library is available on most operating systems, including Windows, MacOS, and Linux, which makes Tkinter a cross-platform library.

In addition to the basic GUI widgets, Tkinter also provides a variety of other features, such as support for geometry management (e.g., pack, grid, place), event handling, and image handling. Tkinter is a standard library and comes pre-installed with Python. It’s simple to use and easy to learn making it a great choice for creating small to medium-sized GUI applications.

 

List of Python TKinter Widgets with Examples

Tkinter is a module in Python that provides a set of tools for creating graphical user interfaces (GUIs). Here is a list of some of the commonly used Tkinter widgets:

  1. Button: A button widget is used to display a button that the user can click to perform an action.
  2. Label: A label widget is used to display a piece of text or an image.
  3. Entry: An entry widget is used to display a single line of text for the user to enter.
  4. Text: A text widget is used to display multiple lines of text, with scrollbars if necessary.
  5. Frame: A frame widget is used as a container for other widgets, to organize them into groups.
  6. Checkbutton: A checkbutton widget is used to display a toggleable checkbox.
  7. Radiobutton: A radiobutton widget is used to display a set of mutually exclusive options, where only one can be selected at a time.
  8. Listbox: A listbox widget is used to display a list of items that the user can select from.
  9. Combobox: A combobox widget is used to display a drop-down list of items that the user can select from.
  10. Scrollbar: A scrollbar widget is used to provide a way to scroll through a list of items or a large piece of text.
  11. Menu: A menu widget is used to create pull-down or pop-up menus.
  12. Message: A message widget is used to display a multiline text or an image with the capability of specifying the aspect ratio of the widget.
  13. Canvas: A canvas widget is used to draw shapes, lines, and other graphics on the screen.
  14. Spinbox: A spinbox widget is used to display a numerical value that the user can increment or decrement using up and down arrows.
  15. Scale: A scale widget is used to display a slider that the user can use to select a numerical value.

These are just a few examples of the many Tkinter widgets available. You can use them in combination to create complex and powerful user interfaces.

 

 

Here are some simple examples of how to use some of the Tkinter widgets:

 

TKinter Button

This code creates a button widget with the text “Click me!” and a command that prints “Button clicked!” to the console when the button is clicked. The button is then packed and added to the root window.

 

 

Tkinter Label

This code creates a label widget with the text “Hello World!” and packs it into the root window.

 

 

TKinter Entry

This code creates an Entry widget and packs it into the root window, which allows user to enter the text.

 

 

TKinter Checkbutton

This code creates a checkbutton widget with the text “Check me!” and a variable that is an instance of IntVar. When the checkbutton is clicked, the variable will be set to 1 (checked) or 0 (unchecked).

 

 

TKinter Radiobutton

This code creates two radiobutton widgets with the text “Option 1” and “Option 2” respectively. They are grouped together by sharing the same variable of the type StringVar. When one of the radio buttons is selected, the variable will be set to the corresponding value.

 

 

TKinter Text

This code creates a Text widget and inserts the text “Hello World!” into it. The text widget is then packed and added to the root window.

TKinter Frame

This code creates a Frame widget, packs it into the root window, and then creates a Label widget and packs it into the Frame.

 

 

TKinter Listbox

This code creates a Listbox widget, inserts three items “Item 1”, “Item 2” and “Item 3” into it and packs it into the root window.

 

 

TKinter Scrollbar

This code creates a Frame widget, packs it into the root window. Within the frame, it creates a Listbox widget and packs it on the left side. It then creates a Scrollbar widget, sets the command attribute to the listbox’s yview method and the listbox’s yscrollcommand attribute to the scrollbar’s set method. Finally, it packs the scrollbar to the right side of the frame and fills it in the Y direction.

 

 

TKinter Menu

This code creates a Menu widget, adds a cascade menu “File” and adds “Open” and “Save” commands to the file menu. The menu is then added to the root window.

 

TKinter Message

This code creates a Message widget, with the text “Hello World!” and packs it into the root window.

TKinter Canvas

This code creates a Canvas widget with width and height of 200 pixels. Then it packs it into the root window, and creates a blue rectangle on the canvas starting from 50,50 to 150,150.

 

 

TKinter SpinBox

In this example, we first create the main window using the Tk() class. Then we create a Spinbox widget with a range from 0 to 100. The widget’s value is stored in the spinbox_var variable. After that, we create a Scale widget with a range from 0.0 to 1.0, with the widget’s value stored in the scale_var variable. Both widgets are then packed into the main window and the application enters the main event loop.

 

 

TKinter Scale

 

In this example, we first create the main window using the Tk() class. Then we create a Scale widget with a range from 0 to 100, with the widget’s value stored in the scale_var variable. This scale widget has a label volume, resolution of 5 and length 200. Both widgets are then packed into the main window and the application enters the main event loop. Also, the on_scale_change function is called whenever the value of the scale changes and it prints the value of the scale.

 

 

Run Python Tkinter Scale Code and This will be the Result.

List of Python TKinter Widgets with Examples
List of Python TKinter Widgets with Examples

 

 

Learn More on TKinter GUI

Leave a Comment