
Getting started with tkinter - crash course
6 January, 2022
4
4
0
Contributors
What is GUI? 🤔
GUI stands for Graphical user interface. Unlike text based interfaces (terminals), GUI uses graphical and interactive components for the user to interact with.
What is tkinter?
- tkinter is an easy to use and standard GUI for Python.
- tkinter stands for Tk interface.
- tkinter is supported by most platforms including Linux, WIndows and macOS.
Summary:
The topics covered in this tutorial are:
- Tkinter widgets
- Tkinter message boxes
- Tkinter simple dialogs
Before you start:
Make sure that the following requirements are installed:
- Python 3
- tkinter
Installation instructions for Linux.
- Run the following commands in your terminal:
- Ensure proper installation of tkinter by running this command in terminal:
- A window should popup after running this command.
Installation instructions for Windows.
- tkinter can be downloaded using Python installer. Run the following command in cmd after installing tkinter to ensure proper installation:
- A window should popup after running this command.
Let's start!
- All components of a GUI are placed inside a window. Here is the code to create a blank tkinter window:
Widgets
The 3 basic widgets in tkinter are:
- Label - Used to display text.
- Button - Interactive widget on which user can click on.
- Entry - Interactive widget to get user input.
Label
The Label widget in tkinter can be used to display text of various sizes and styles. Here is the code to use Label widget in a window:
Properties of a Label widget.
The major properties of a Label widget are:
-
Text
-
Background colour
-
Foreground colour
-
Font family and size
Here is the code to make a Label widget with custom properties:
Button
The Button widget in tkinter can be used to call a command when clicked. Here is the code to use Button widget in a window:
Properties of a Button widget.
The major properties of a Button widget are:
-
Text
-
Background colour
-
Foreground colour
-
Font
-
Command
Usage is the same as Label widget. Arguements can be passed.
Entry
The Entry widget in tkinter can be used to get user input graphically. The text entered in an Entry widget can be obtained by get()
method. Here is the code to use Entry widget along with Button widget in a window:
Properties of an Entry widget:
The major properties of an Entry widget are:
-
Background colour
-
Foreground colour
-
Font family and size
Usage is the same as Label widget. Arguements can be passed.
Message boxes
Other than widgets, Message boxes can be used in tkinter for basic prompts. Message boxes cannot be used without a main window. The Message boxes available in tkinter can be classified into two types based on usage:
- Message boxes for showing information.
- Message boxes for asking questions.
Message boxes for showing information.
There are three message boxes for showing information. The code below explains them.
Message boxes for asking questions.
There are two message boxes for asking questions. The code below explains them.
Simple dialogs.
Simple dialogs are like Message boxes and they too require a main window to run. Unlike Message boxes, Simple dialogs are used for getting input(strings, integers and floats). The code below explains how to get input using a simpledialog.
Thank you!
python
tutorial
introduction
tkinter
beginner