Deploy Containers Close to Your Users

This Engineering Education (EngEd) Program is supported by Section.

Instantly deploy containers globally. Section is affordable, simple and powerful.

Get Started for Free.

Introduction to GUI in Python using Tkinter

June 29, 2021

A graphical user interface (GUI) allows users to interact with electronic devices using electronic symbols, as opposed to text-based user links, typed commands, or text exploration. GUIs were designed in response to command-line networks, an apparent steep learning curve, which requires command input via a computer keyboard.

In most cases, activities in a GUI are performed by directly manipulating the graphical elements. GUIs are utilized in a range of mobile gadgets, including MP3 players, gaming devices, workplaces, and industrial controllers, in addition to computers.

In this tutorial, we will gain an understanding of how to build a password generator GUI using Tkinter.

Table of content

What is Tkinter?

Building GUIs with Tkinter has several advantages. Because Python is a cross-platform, apps built with Tkinter can run on Windows, Mac OS X, and Linux.

Tkinter applications appear to be indigenous to the instance on which they run. This is because the visual components are built with native system applications.

Python must be installed before Tkinter can be used. When you install Python, you get Tkinter as well. However, if after installing Python Tkinter was not installed, you can use the pip command to install it:

pip install tk

This command will install the Tkinter library and its related libraries.


To follow along with this tutorial, you should have:

What are widgets?

Widgets are the building blocks of graphical user interface programming, and they are used to view data or collect input from the user. See some examples of commonly used widgets below:

  • Frame: It is a container that organizes and borders other widgets.
  • Label: The label widget is used to display text on the screen.
  • Button: It can contains text and when a button is clicked it executes an action.
  • Entry: It displays a single-line text field for accepting user input.
  • Text: A text entry widget allows you to enter multi-line text.

what is a password generator?

A password generator is software that accepts input from a random or pseudo-random string or number generator and uses it to generate a password for the user.

It is a program that creates passwords depending on the rules the user selects to build complicated passwords that are tough to predict or breach for multiple accounts.

Online hacking is growing more common these days, resulting in passwords and login information being compromised regularly, loss of money, and delicate personal information.

Unfortunately, the majority of stolen passwords are uncomplicated and easy to guess. Names, date of birth, name of your spouse or family member, and personal pet names should not be used as passwords, because they are easy to guess. Password generators are very useful in these circumstances.

Building a password generator

Create a new file named where we will write our code.

Let’s start by importing Tkinter:

from tkinter import Button, Entry, Label, StringVar, Tk
from tkinter.ttk import Combobox
import random

The tkinter.ttk module gives the user access to the Tkinter widget set and allows the user to import Combobox. A combobox is a graphical user interface element that merges a drop-down box, list box, and allows the user to select input.

screen = Tk()
screen.title("Password Generator")
screen.configure(background ="red")

A root window is created using the Tk class. The Tk() function assists in the creation of this GUI window, and provides numerous options such as setting the title and the geometry of the GUI window.

The geometry() method is one of the many methods provided by Tkinter. We changed the background color to red using the screen.configure() method.

def gen():
   global sc1

We defined the global variable sc1. We used the .set() method to set the value of sc1 to an empty string. We also assigned an empty string value to the passw variable.

The length variable will retain the value of c1 which will determine the total number of values (alphabets, numbers, and symbols) required to form any password as selected by the user.

We then created a lowercase variable that holds a string value of all the alphabets in lowercase. After that, we created the uppercase variable to hold a string value of all the alphabets in uppercase.

In the last line, we defined the mixs which holds the values for all integer values, alphabets in both uppercase and lowercase, signs and symbols that will be required in creating a high-length password.

Add the following code inside the gen() function:

if c2.get()=='Low Strength':
    for i in range(0,length):

elif c2.get()=='Medium Strength':
    for i in range(0,length):

elif c2.get()=='High Strength':
    for i in range(0,length):

We defined the if condition to monitor the strength of the passwords generated. If the user decides the length of the password and then clicks on Low Strength, the password generator generates a random password in lowercase.

If the user clicks on Moderate Srength, the password generator generates a random password in both lowercase and uppercase. This decreases the chances of an intruder predicting the password.

If the user clicks on High Strength, the password generator generates a random password that is a mixs containing uppercase, lowercase, and special alphabetical signs and symbols. This gives an intruder little or no chance of predicting the password.

Add the following code outside the gen() function:

t1=Label(screen,text='Password Generator',font=('Arial',26),fg='white',background ="red"),y=0)

t2=Label(screen,text='password:',font=('Arial',16),background ="red"),y=90)

The variable sc1 has been defined. In order to design the GUI window’s section in this project, we’ll use a label, an entry box, and a button.

On the GUI window, we have created a label t1 which is the title of the label item. We type the text on the label. We have also changed the font and background of the label. Then by using the x and y coordinate values, we have assigned the location of the t1 label.

We have also created a label t2, its placeholder text, and its background. And we set its coordinates.

Add the code below:


t3=Label(screen,text='Length: ',font=('Arial',16),background ="red"),y=130)

t4=Label(screen,text='Strength:',font=('Arial',16),background ="red"),y=155)


c2['values']=('Low Strength','Medium Strength','High Strength')

b=Button(screen,text='Generate',font=('Arial',16),fg='red',background ="white",command=gen),y=195)


Using the object il, we defined the entry box. We have calibrated the font of il and put the text variable to store the string worth of sc1. We have used x and y coordinates to assign the location of the il entry. We defined the label t3. Then, using coordinate values, we assigned it a location.

We then created the t4 label. Using coordinates values, we assigned the location. We also gave entry c1 a name. Set the Combobox c2’s values to “Low Strength”, “Medium Strength”, and “High Strength”. The current() function is used to set the value of c2.

Then, using x and y coordinate values, we assigned the location of the c2 Combobox. We defined b as a button and used the generator command to build an action when the button is clicked.

Finally, we used x and y coordinates to assign the location of the button b. The mainloop() function is an infinite loop that is used to run the program.

To run our app, execute the following command on your terminal:

$ python

And the password generator interface will be:

password image

Congratulations on building your first Tkinter application!


In this tutorial, We covered some fundamentals of the Tkinter GUI, and we built a password generator application in Python using Tkinter. You can find the code on github.

Happy Coding!

Further reading

Peer Review Contributions by: Geoffrey Mungai