Coders Packet

Length Converter Using GUI Tkinter in Python

By Aanchal Kaushal Sharma

The code will make a GUI window in Python that would ask a user to input the length. The user will get values in different units when asked to convert.

In this tutorial, we will be making a GUI window using Tkinter GUI in python. We will go through the basic libraries and concepts and codes in Tkinter.

What this code does is, it will make a GUI window that would ask a user to input length in meters, when done and asked to convert would display values of lengths in different units.

GUI stands for Graphical User Interface, which helps us the users to interact with the machine in a more fun way or through an interface. Tkinter is the method that provides a toolkit for GUI.

As we are starting with the code some steps are to be remembered.

  1. Importing the module Tkinter
  2. Initializing a variable in Tkinter
  3. Creating a window
  4. Adding widgets to it
  5. Closing through a variable in mainloop()

Importing the module

from tkinter import* 

Includes all the names of Tkinter to the code

 

Initializing the variable

window=Tk()
 

To create the main window, Tkinter offers a method TK(). 

 

Creation of window

You can add title and window geometry to add details to your window.

 

 Adding widgets to it

In this code the widgets used are

Button: this is used to place buttons in Tkinter

Label: A single line widget that displays a box where text can be inserted

Entry: This is the input widget, any single line input is accepted here.

Text: Inserts text in Tkinter

 

Closing through a variable in mainloop()

This is the final closing statement in your code. It tells python to run the code in loops until the window is closed.

 

Getting started with the code

After creating a window, using below code

from tkinter import *

# Create a GUI window

window = Tk()

window.title("Lenght Converter")

window.geometry("500x300+550+355")

We need to place widgets for which we would use the label as

e10 = Label(window, text = 'dm',bg="light gray")

here, we can insert text to our label added with color

Also, text widget is used as

t8 = Text(window, height = 1, width = 15)

Here, height and width can be added to your text

The button widget is used as

exit_button = Button(window, text="Exit", command=window.destroy)

exit_button.place(x=450,y=255)

 

Here, the button widget is used as an exit to the window. When the button is clicked, the application terminates.

 

Defining of length converter function

Initializing variables and converting them to their values is done in function.

def lengthconv():

    

    km = float(e2_value.get())*1000

    mm = float(e2_value.get())/1000

    cm = float(e2_value.get())/100

    mile=float(e2_value.get())*0.00062

    yard=float(e2_value.get())*1.094

    inch=float(e2_value.get())*39.37

    foot=float(e2_value.get())*3.281

    dm=float(e2_value.get())*10

 

Complete code:

from tkinter import *
# Create a GUI window
window = Tk()
window.title("Lenght Converter")
window.geometry("500x300+550+355")
def lengthconv():
     
    km = float(e2_value.get())*1000
    mm = float(e2_value.get())/1000
    cm = float(e2_value.get())/100
    mile=float(e2_value.get())*0.00062
    yard=float(e2_value.get())*1.094
    inch=float(e2_value.get())*39.37 
    foot=float(e2_value.get())*3.281
    dm=float(e2_value.get())*10
    
    t1.delete("1.0", END)
    t1.insert(END,km)
     
    t2.delete("1.0", END)
    t2.insert(END,mm)
     
    t3.delete("1.0", END)
    t3.insert(END,cm)
    
    t4.delete("1.0", END)
    t4.insert(END,mile)
   
    t5.delete("1.0", END)
    t5.insert(END,yard)
    
    t6.delete("1.0", END)
    t6.insert(END,inch)
    
    t7.delete("1.0", END)
    t7.insert(END,foot)
    
    t8.delete("1.0", END)
    t8.insert(END,dm)
    
# Creating Label widgets
e1 = Label(window, text = "Enter length in meters",bg="light gray")
e2_value = StringVar()
e2 = Entry(window, textvariable = e2_value)
e3 = Label(window, text = 'km',bg="light gray")
e4 = Label(window, text = 'mm',bg="light gray")
e5 = Label(window, text = 'cm',bg="light gray")
e6 = Label(window, text = 'mile',bg="light gray") 
e7 = Label(window, text = 'yard',bg="light gray") 
e8 = Label(window, text = 'inch',bg="light gray") 
e9 = Label(window, text = 'foot',bg="light gray") 
e10 = Label(window, text = 'dm',bg="light gray") 
# Creating Text Widgets
t1 = Text(window, height = 1, width = 15)
t2 = Text(window, height = 1, width = 15)
t3 = Text(window, height = 1, width = 15)
t4 = Text(window, height = 1, width = 15) 
t5 = Text(window, height = 1, width = 15) 
t6 = Text(window, height = 1, width = 15)
t7 = Text(window, height = 1, width = 15)
t8 = Text(window, height = 1, width = 15)
# Creating  Button Widget
b1 = Button(window, text = "Convert", command = lengthconv)
 
e1.grid(row = 0, column = 0,padx=10,pady=10)
e2.grid(row = 0, column = 1,pady=10,padx=10)
e3.grid(row = 4, column = 0,pady=10,padx=10)
e4.grid(row = 2, column = 0,pady=10,padx=10)
e5.grid(row = 3, column = 0,pady=10,padx=10)
e6.grid(row = 2, column = 2,pady=10,padx=10)
e7.grid(row = 4, column = 2,pady=10,padx=10)
e8.grid(row = 3, column = 2,pady=10,padx=10)
e9.grid(row = 5, column = 0,pady=10,padx=10)
e10.grid(row = 5, column = 2,pady=10,padx=10)
t1.grid(row = 4, column = 1,pady=10,padx=10)
t2.grid(row = 2, column = 1,pady=10,padx=10)
t3.grid(row = 3, column = 1,pady=10,padx=10)
t4.grid(row = 2, column = 3,pady=10,padx=10)
t5.grid(row = 4, column = 3,pady=10,padx=10)
t6.grid(row = 3, column = 3,pady=10,padx=10)
t7.grid(row = 5, column = 1,pady=10,padx=10)
t8.grid(row = 5, column = 3,pady=10,padx=10)
b1.grid(row = 1, column = 1,padx=10,pady=10)
 
exit_button = Button(window, text="Exit", command=window.destroy) 
exit_button.place(x=450,y=255)

window.mainloop()

 

Output:

 

Output Image of length converter

Download project

Reviews Report

Submitted by Aanchal Kaushal Sharma (AanchalShar)

Download packets of source code on Coders Packet