Start With Python Tkinter

In this Python tutorial, I will introduce you to discuss Python GUI programming library used to create desktop applications. I will show what Python GUI is and what Python TKinter is, and how to use Tkinter in Python. I’ll walk you through the basics of setting up a GUI window, adding widgets like buttons and labels, etc.

Let’s start..

GUI Using Python

  • If you are a Python developer and want to create cool applications that support Graphical User Interface then you are in the right place. Here, we will learn how to create GUI-based applications using Python Tkinter. The word “Tkinter” is pronounced as “T-kin-ter “. We will read all about Python GUI programming.
  • In Python, Tkinter is used for creating Software whereas Turtle & Pygame are used for creating Graphic based animations & games.

Python Tkinter Installation

  • Python comes with a built-in Tkinter module so if you have Python installed you can execute the Tkinter program.
  • In case you want to know, how to install Python, then check Python download and Installation steps.
  • Type python -m tkinter on command-Prompt to quickly check for Tkinter. If the below kind of window appears, that means Tkinter is installed & you can skip this step.
Python GUI Programming (Tkinter) quick check
python gui programming
  • In case these windows don’t appear, then follow these steps: (Macintosh & Linux users can also follow these steps)

Step-1:

Check the version of Python installed in the system: python --version

Python GUI Programming (Tkinter) check python version

Step-2:

Go to python.org and download the same version of python. In my case, I have downloaded Python 3.7.9 for 64-bit.

Step-3:

Click on the installation file >> window will appear >> click on Modify.

Python GUI Programming (Tkinter) modify
python gui programming

Step-4:

Select the third option that reads Tcl/tk and IDLE , and then click Next, followed by install.

Python GUI Programming (Tkinter) install
Python tkinter installation

Step-5:

Type python -m tkinter on command-Prompt. If this window appeared that means tkinter is installed now.

Python GUI Programming (Tkinter) quick check
Python GUI Programming

Read Upload a File in Python Tkinter

Create First Program using Tkinter

Let me help you understand a bit more about Python GUI programming.

Before introducing any command, I want you to copy and paste the code into your code editor and try it once. This is a simple “Hello” program that will give an idea about what we are going to do.

python guides images\tkinter\Python GUI Programming (Tkinter) first program.png

Code:

# import modules
from tkinter import *

# configure workspace
ws = Tk()
ws.title("First Program")
ws.geometry('250x150')
ws.configure(bg="#567")

# function territory
def welcome():
    name = nameTf.get()
    return Label(ws, text=f'Welome {name}', pady=15, bg='#567').grid(row=2, columnspan=2)

# label & Entry boxes territory
nameLb = Label(ws, text="Enter Your Name", pady=15, padx=10, bg='#567')
nameTf = Entry(ws)

# button territory
welBtn = Button(ws, text="ClickMe!", command=welcome)

# Position Provide territory
nameLb.grid(row=0, column=0)
nameTf.grid(row=0, column=1)
welBtn.grid(row=1, columnspan=2)

# infinite loop 
ws.mainloop()
  • This program is just for reference. By the end of this blog, you will be able to write GUI-based software.
  • Before moving forward, let’s understand the flow of the program for tkinter-based applications.
Python GUI Programming (Tkinter) program flow
python tkinter example

The above-mentioned program is done in the same sequence & this sequence is followed in every Tkinter beginner program. This keeps the code clean and organized.

  1. Import the necessary modules or libraries.
  2. Workspace is created using: ws = Tk(), here “ws” can be replaced with any name. The most common ws replacements are root, window, win, master, etc. It is completely for your reference; in our case, it means workspace.
  3. An infinite loop is necessary to hold the screen. Without this loop, the screen will splash & disappear immediately.
  4. Label, entry boxes & buttons are the widgets. We will study them in detail later in the course.
  5. Button territory holds all the buttons.
  6. The widget has 3 parts as shown in the picture.
Python GUI Programming (Tkinter) widget formation
Python GUI programming.
  • Variable is the name assigned to the widget. All the activities performed on the widget will be stored under this name. This name can be used later to refer to this widget.
  • Widgets are Label, Entry, Button, message, Check button, Radio-button, etc. They have a dedicated task to perform. Apart from the task, they can be configured with additional utilities like color, font, etc.
  • Geometry refers to the position of a widget. Tkinter has 3 types of geometry (pack, grid, place). Each of them has a different way of placing a widget. Will know about them in detail in a later course.

Read Navigate Between Pages in a Python Tkinter

Python Tkinter widgets

  • GUI is all about widgets, as Python Tkinter widgets provide us controls using which user interacts with our application.
  • Label, Text boxes, List boxes, Buttons, Menu, etc are known as widgets. Each widget has its property.
  • There are 10 types of Python Tkinter widgets that I have explained individually one by one (with examples).
  • ws represents the parent window.

Python tkinter Label

  • A Python tkinter label is a simple piece of text or information.
  • The label is a very common & widely used widget.
  • The label is the first widget used before creating any application.

Syntax:

Label(ws, text=value )

Example:

This is the implementation of Label. Here, the text is assigned a value, “Enter Name”.

Code:

from tkinter import *

ws = Tk()

nameLb = Label(ws, text="Enter Name")
nameLb.pack()

ws.mainloop()

Output:

Enter Name was the value assigned in the label, so it is being displayed here.

Python Tkinter label widget
Python tkinter example

Check out Implement Drag and Drop Functionality in Python Tkinter

Python tkinter Entry

Let us discuss Python tkinter Entry.

  • Entry boxes are used to take user input.
  • This is another widely used widget.

Syntax:

Entry(ws)

Example:

This is the implementation of Entry boxes. It will display an empty box ready to be filled by the user.

Code:

from tkinter import *

ws = Tk()

nameTf = Entry(ws)
nameTf.pack()

ws.mainloop()

Output:

The output displays an empty text box where “Type Text Here!” is written by the user.

Python Tkinter widget Entry box
Python tkinter Entry

Read Create a Date Time Picker using Python Tkinter

Python tkinter Button

Let me discuss Python tkinter Button.

  • The button triggers an action.
  • The button takes command as an argument that does all the magic.

Syntax:

Button(ws, command=<action>)

Example:

This is the implementation of Button. display text provided is “submit” and the command passed is function: Submit. On clicking the button function will be triggered.

Code:

from tkinter import *

ws = Tk()

def submit():
    return Label(ws, text="Submitted!").pack()

submitBtn = Button(ws, text="submit", command=submit)
submitBtn.pack()

ws.mainloop()

Output:

The function was to create another label with the text “Submitted“. So the output shows the small text below the button. It is created after the button is clicked.

Python Tkinter widget Button
Python Tkinter Button

Read Display Data in Textboxes using Python Tkinter

Python tkinter message box

  • The Python message box is a pop-up box with a message.
  • To use the message box, import the message box module.
  • There are six types of message prompts, such as:
  • askquestion: Prompts “Yes”, “No” options. Returns Yes or No.
  • askyesno: Prompts with “Yes”, “No” options. Yes returns 1 and No returns 0.
  • askretrycancel: Prompts with the option to “retry” or “cancel. With sound, Retry returns 1, and cancel returns 0.
  • showerror: Displays an error message with sound. returns “Ok”.
  • showinfo: Displays a message with sound. returns “Ok”.
  • show warning: Displays warning message with sound. returns “Ok”.

Syntax:

messagebox.function(title, message)

Example:

Here is an example of all the message boxes using Python tkinter. All types of messages are put under one function that will be triggered with one button.

Code:

from tkinter import *
from tkinter import messagebox

ws = Tk()

def prompts():
    messagebox.showinfo("showinfo", "Hi there!")
    messagebox.showwarning("showinfo", "Hi there!")
    messagebox.showerror("showinfo", "Hi there!")
    messagebox.askquestion("showinfo", "Hi there!")
    messagebox.askyesno("showinfo", "Hi there!")
    messagebox.askretrycancel("showinfo", "Hi there!")
    
Button(ws, text="Click Me!", command=prompts).pack()

ws.mainloop()

Output:

The moment Click Me! button is pressed, these prompts start appearing in the sequence.