1 - The graphical User Interface (GUI) tkinter for Python

So far, we have only seen the Python programming under console, we can then wonder how can we manipulate windows with buttons, labels and input fields ...
Well, know that the developers of the Python language, have already thought about that. Python already integrates with its interpreter a free graphic library named Tkinter. Creating your Human Machine Interfaces with this library will allow you to have no library to download in addition to your code.
But if you want to integrate other graphic libraries, here are the most famous bibs:

  • Tkinter for Tk
  • wxPython for wxWidgets
  • PyGTK for GTK +
  • PyQt for Qt.

Note that each of these libraries has its own characteristics. As for us in this tutorial, we will limit ourselves to tkinter which is integrated in the Python language.

2 - Create a window with tkinter

2 - 1 - Create a simple window with tkinter

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a quick and easy way to create graphical applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolbox.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps:
  • Import the Tkinter module.
  • Create the main window of the GUI application.
  • Add one or more of the widgets mentioned above to the GUI application.
  • Enter the main event loop, mainloop()

Here's the code that create a simple window:

from tkinter import *
my_window = Tk()
my_window.mainloop()

What displays after execution:

2 - 2 - Add a name to the window

To add a name to the window just add the instruction:

my_window.title ("my first window") 

Here is the code that creates a window with a name: "my first window"

from tkinter import *
my_window = Tk()
my_window.title("my first window")
my_window.mainloop()

What displays after execution:

2 - 3 - Define the dimensions of the tkinter window

To define the dimensions of the window, just use the geometry() method by adding the statement:

my_window.geometry ("width x height")

Here is the code that displays a window named  "my first window" and dimension 350 x 200 :

from tkinter import *
my_window = Tk()
my_window.title("my first window")
my_window.geometry( "350x200")
my_window.mainloop()

What displays after execution:

3 Tkinter widgets

The Tkinter library provides various controls, such as buttons, labels, and text boxes used in a graphics application. These controls are commonly called widgets.
There are currently 15 types of widgets in Tkinter. Here we present the names of these widgets and a brief description:

  1. Button: the Button widget allows you to create buttons for your application.
  2. Canva: the Canva widget allows you to draw shapes, such as lines, ovals, polygons and rectangles, in your application.
  3. Checkbutton: the Checkbutton widget allows you to display a number of options in the form of check boxes. The user can select several options at the same time.
  4. Entry: the Entry widget is used to display a single line text field for accepting values ​​from a user.
  5. Frame: the Frame widget is used as a container widget to organize other widgets.
  6. Label: the Label widget is used to provide a legend or description for the other widgets. It can also contain images.
  7. Listbox: The Listbox widget is used to provide a list of options to a user.
  8. menubutton: the menubutton widget is used to display the menus in your application.
  9. Menu: The Menu widget is used to provide various commands to a user. These commands are contained in Menubutton.
  10. Message: the Message widget is used to display multi-line text fields allowing to accept the values ​​of a user.
  11. Radiobutton: the Radiobutton widget is used to display a number of options in the form of radio buttons. The user can only select one option at a time.
  12. Scale: The Scale widget is used to provide a cursor widget.
  13. Scrollbar: The Scrollbar widget is used to add scrolling functionality to various widgets, such as list boxes.
  14. Text: the Text widget is used to display text on multiple lines.
  15. Toplevel: the Toplevel widget is used to provide a separate window container.
  16. Spinbox: the Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select a fixed number of values.
  17. PanedWindow: the PanedWindow widget is a container that can contain any number of panes, arranged horizontally or vertically.
  18. LabelFrame: a labelframe is a simple container widget. Its main purpose is to act as a divider or container for complex window layouts.
  19. tkMessageBox: this module is used to display message boxes in your applications.

Younes Derfoufi
my-courses.net
5 thoughts on “The Graphical User Interface GUI Tkinter in Python”

Leave a Reply