Getting Started with Python GUI Programming using Tkinter

Umesh S
4 min readFeb 25, 2023

--

Photo by David Clode on Unsplash

Python is a popular programming language that can be used for a variety of applications, including the creation of graphical user interfaces (GUIs).

One of the most commonly used GUI toolkits for Python is Tkinter, which provides a simple way to create desktop applications with a graphical interface. In this guide, we will walk through the basics of creating a GUI application with Tkinter.

Step 1: Install Tkinter : Tkinter is included in the standard Python distribution, so you should already have it installed on your system. However, if you don’t have it installed, you can install it using your operating system’s package manager.

Step 2: Import the Tkinter Module To use Tkinter, you need to import the Tkinter module in your Python script. You can do this using the following code:

import tkinter as tk

This imports the Tkinter module and renames it to “tk” for convenience.

Step 3: Create the Main Window: The main window is the top-level container for your GUI application. To create the main window, you can use the Tk() function from the Tkinter module, like this:

root = tk.Tk()

This creates a new window and assigns it to the variable “root”.

Step 4: Add Widgets to the Main Window: Widgets are the graphical elements that make up your GUI application. You can add widgets to the main window using various Tkinter classes. For example, to add a label to the main window, you can use the Label class:

label = tk.Label(root, text="Hello, world!")
label.pack()

This creates a new label with the text “Hello, world!” and adds it to the main window using the pack() method.

Step 5: Respond to User Input: GUI applications typically respond to user input, such as mouse clicks or keyboard input. To handle user input, you can use event binding. For example, to bind a function to the button click event, you can use the following code:

def button_click():
print("Button clicked!")

button = tk.Button(root, text="Click me!", command=button_click)
button.pack()

This creates a new button with the text “Click me!” and binds the button_click() function to the button click event using the command parameter.

Step 6: Run the Main Loop: The main loop is a blocking loop that processes user input and updates the GUI as necessary. To start the main loop, you can use the following code:

root.mainloop()

This starts the main loop and blocks the program until the main window is closed.

Creating Widgets

Widgets are the building blocks of a GUI. Tkinter provides a variety of widgets to create a graphical user interface. Here are some commonly used widgets:

  • Label: Displays text or an image.
  • Button: Executes a command when clicked.
  • Entry: A single-line text field that allows user input.
  • Text: A multi-line text field that allows user input.
  • Frame: A container widget that groups other widgets together.
  • Canvas: A widget used to draw graphics and shapes.
  • Listbox: A widget that displays a list of items.
  • Menu: A widget that creates a menu or dropdown list.

To create a widget, you need to call the constructor of the widget class and pass the parent widget as the first argument. Here is an example of creating a Label widget:

from tkinter import *

root = Tk()

label = Label(root, text="Hello, World!")
label.pack()

root.mainloop()

In this example, we create a Label widget with the text “Hello, World!” and add it to the root window using the pack() method.

Handling Events

Events are actions performed by the user, such as clicking a button or entering text in a field. To handle events in Tkinter, you need to bind the event to a function that will be executed when the event occurs.

Here is an example of binding a function to a Button widget:

from tkinter import *

root = Tk()

def on_button_click():
print("Button clicked!")

button = Button(root, text="Click me", command=on_button_click)
button.pack()

root.mainloop()

In this example, we define a function called on_button_click() that will be executed when the Button widget is clicked. We then create a Button widget and pass the function as the command argument.

Layout Management

Layout management is the process of arranging widgets on a window or frame. Tkinter provides several layout managers, including pack(), grid(), and place().

  • pack() is a simple layout manager that places widgets in a horizontal or vertical box.
  • grid() is a more flexible layout manager that places widgets in a grid of rows and columns.
  • place() is the most flexible layout manager that allows you to specify the exact position and size of a widget.

Here is an example of using the pack() layout manager:

from tkinter import *

root = Tk()

label1 = Label(root, text="Label 1")
label1.pack()

label2 = Label(root, text="Label 2")
label2.pack()

label3 = Label(root, text="Label 3")
label3.pack()

root.mainloop()

In this example, we create three Label widgets and add them to the root window using the pack() method. The pack() method arranges the widgets in a vertical box.

Styling Widgets

You can customize the appearance of widgets by changing their colors, fonts, and other properties. Tkinter provides several methods to modify the appearance of widgets, such as config() and configure().

Here is an example of changing the font and background color of a Label widget:

from tkinter import *

root = Tk()

label = Label(root, text="Hello, World!", font=("Arial", 16), bg="red")
label.pack()

root.mainloop()

In this example, we create a Label widget with a custom font and background color using the font and bg arguments.

--

--

Umesh S
Umesh S

Written by Umesh S

Experienced Software Engineer committed to helping others grow and succeed.

No responses yet