This tutorial explains how to create a Python GUI with Tkinter
Introduction to Python GUI
The GUI or Graphical User interface is a form of user interface that includes graphical elements, such as windows, icons and buttons. These allow the average user to communicate with an electronic device. The alternative is to use the command line, which is neither user friendly or visually pleasing.
Every programming language has it’s own set of unique GUI libraries. In Python, Tkinter is the most common GUI Library used to create Graphical User Interfaces.
Why use Tkinter?
Tkinter is an open source Python GUI library known for it’s simplicity and flexibility. It comes pre-installed in Python 3 so you don’t even have to worry about downloading it. These qualities make it a strong starting point for the beginner and intermediate levels. Not to say that tkinter can’t be used on larger scale projects. If you master tkinter, you can produce GUI on par with other more complex and modern GUI libraries.
There are many alternatives to Tkinter such as PyQt5 which is known for it’s more advanced widgets and modern look. You can learn PyQt5 using our very own PyQt5 tutorial series if you wish to, here on CodersLegacy.
Tkinter Widget Compilation:
Tkinter utilizes “widgets” which refer to individual GUI elements like the entry widget, messagebox widget and checkbox widget. Each widget further has over a dozen different options and many ways to use and manipulate them. We’ll be covering just about all the widgets in the Tkinter GUI Library (around a dozen). One page will be dedicated to each widget and it’s usage.
Python Tkinter Widgets:
Introduction to Tkinter + Frame Widget: Outlines the frame for your Tkinter window with a fixed size. Just like the human skeleton, a Tkinter window requires a frame to support it and give it structure.
Buttons: The Python Tkinter Button is a standard Tkinter widget. A button is used as a way for the user to interact with the User interface. Once the button is clicked, an action is triggered by the program.
Entry: A standard Tkinter widget used to take input from the user through the user interface. A simple box is provided where the user can input text.
Label: A Tkinter widget used to display simple lines of text on a GUI.
Menu: The Tkinter Menu widget is used to create various types of menus in the GUI such as top-level menus, which are displayed right under the title bar of the parent window.
ComboBox: A special extension of Tkinter,
ttk module brings forward this widget. A combobox presents a drop down list of options and displays them one at a time. Has a more modern approach than other similar widgets.
List Box: Another Tkinter widget that is used to display a list of options for the user to select. Displays all of the options in one go. Furthermore, all options are in text format.
Menu Button: A combination of both the button and menu widget, this button widget displays a drop down menu with a list of options once clicked.
Canvas: One of the more advanced Tkinter widgets. As the name suggests, it’s used to draw graphs and plots on. You can even use images in a Canvas. It’s like a drawing board on which you can paint/draw anything.
Scale: The Tkinter Scale widget is used to implement a graphical slider to the User interface giving the user the option of picking through a range of values.
Scrollbar: A useful widget in GUI’s, which allows you to scroll in a Tkinter window or enable the scroll feature for certain widgets.
Text: An alternative to the Tkinter Entry widget, which can be used to take multiline input from the user. Comes with many extra features such images, tags, marks etc.
File Dialogs: File Dialogs has a rather unique function in Tkinter. They give the user the option to select/save a file/directory from their computer through the use of a “Dialog” (The dialog that appears when you click on “open file” or “save file”). It’s good looking, fast and user friendly and comes in handy often.
Toplevel: A widget in Tkinter that allows for the easy spawning of new Tkinter Windows. Toplevel is a better alternative to spawning extra tkinter windows by using
SpinBox: A variant of the ComboBox widget, which has a list of options which “spin” as you use the arrow keys to navigate.
Tkinter ttk Module
The ttk module comes bundled with 18 widgets, out of which 12 are already present in Tkinter. Importing ttk over-writes these widgets with new ones which are designed to have a better and more modern look across all platforms.
Here is a list of the 6 widgets unique to ttk, and links to their respective tutorials.
Notebook: This widget manages a collection of windows or “tabs” between which you can swap, changing the currently displayed window.
ProgressBar: This widget is used to show progress or the loading process through the use of animations.
Separator: Used to separate different widgets using a separator line.
Treeview: This widget is used to group together items in a tree-like hierarchy. Each item has a textual label, an optional image, and an optional list of data values
ComboBox: Used to create a dropdown list of options from which the user can select one.
Sizegrip: Creates a little handle near the bottom-right of the screen, which can be used to resize the window.
Bonus Tkinter Articles:
A Python GUI library like Tkinter has alot to offer besides the above widgets. Below are some extra features, functions and (minor) widgets that are less common, but are irreplaceable in what they do.
- How to delete widgets in Tkinter
- How to close a Tkinter window (programatically)
- How to center a Tkinter Window on the Screen
- Disable resizing for a Tkinter window
- How does the mainloop() function work?
- Customize Tkinter Font
- Emptying out the Entrybox contents
- Creating a color chooser
- Tkinter Grid Placement
This marks the end of the Python GUI with Tkinter Article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial can be asked in the comments section below.