This tutorial series explains how to create a Python GUI with Tkinter.
Introduction to Python GUI with Tkinter
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 learn tkinter properly, 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 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. Our tutorial series covers every single widget available in the Tkinter GUI Library (around twenty). In-fact, we even include some extra ones which are not included in Tkinter by default!
Each Tkinter widget has a dedicated tutorial you can access by following its respective link.
Python Tkinter Widgets:
Tkinter Introduction + 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.
Check Button: A check button is a Tkinter GUI widget that presents to the user a set of predefined options. The user may select more than one option.
Radio Button: A radio button is a Tkinter GUI widget that allows the user to choose only one of a predefined set of mutually exclusive options.
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.
OptionMenu: The OptionMenu Widget is a simple widget that is used to display a dropdown list/menu of options in a Tkinter window. It has the appearance of normal button.
Tkinter GUI Projects: Follow the link to an article where we discuss and share the code for 5 different Tkinter projects, such as a Syntax Highlighter, Notepad, Login page and more! Learn how to apply Tkinter in real life projects, instead of just knowing the theory!
Tkinter ttk Module
The Tkinter ttk module is a special extension of the popular Tkinter GUI library in Python. ttk basically stands for Themed Tkinter, as it provides pre-built themes and styling support for Tkinter.
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 Tutorials:
A Python GUI library like Tkinter has alot to offer besides the above widgets. Below are some tutorials on extra Tkinter 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
- Paned Windows in Tkinter
- Create a Calendar in Tkinter
Tip: Interested in exploring other GUI Frameworks? Check out our article on the Best GUI Frameworks in Python!
This marks the end of the Python GUI with Tkinter Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial can be asked in the comments section below.