At some point in your programming career, you’ll get tired of having your results output on the console window. This is especially true if you’re trying to make a user-friendly interactive software application. This where the Python GUI comes in.
What is a 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.
For instance, Windows, an extremely common Operating system, is also a form of GUI. Windows provides us with an interface through which we can personalize, access, use and develop our PC’s.
Python GUI Libraries
Python has several third party libraries through which we can create GUI’s. We’ll be discussing two such libraries here right now.
A 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 that. 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 GUI libraries.
Furthermore, tkinter is more desktop oriented in comparison to the mobile platform and has less cross platform support than some other GUI libraries. So if any of things matter greatly to you, you might want to pick another GUI library.
Another great Python GUI library with great cross platform support. The things you learn in PyQt will also translate directly when working with other languages so that’s a significant plus point. It’s known as a stable and mature library with many features. The latest version is PyQt5.
Some downsides to PyQt include the fact that it’s not natively bundled with python, so you have to take care of packaging it yourself. Another issue is that PyQt is only available under the GPL. This basically means that anything you make and want to release using PyQt must be open-source. If you ever want to make a commercial application using this, you’ll have to purchase a commercial license.
Another honorable mention is Kivy which is known to be good for developing for Android and touchscreen devices. If you’re developing a GUI for a mobile app, Kivy is the way to go.
We’ll be going using Tkinter as the library of choice through out the GUI section. Mostly due to it being simpler (and faster) to pick and set up than other libraries. Once you’ve got the hang of it, and you want something more powerful, you can learn PyQt5 from our very own tutorials series here.
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.
See below for a list of some tkinter widgets, links to their respective pages and a brief description for each one of them.
Python Tkinter Widgets:
Frame: 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.
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
Bonus Tkinter Articles:
A large 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)
- Disable resizing for a Tkinter window
- 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 above material can be asked in the comments section below.