×

Weight Conversion GUI using Tkinter in Python

GUI:

One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs are intuitive enough that even relatively unskilled employees without programming experience can use them. Because they are always designed with the user in mind rather than being primarily machine-centered, they have become the standard in software application programming.

 The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's action. Since many GUIs represent text and graphical elements in standard formats, it is possible for programs using the same GUI software to share data. As it is patched and developed, the same software application or os version may present distinct or marginally distinct GUIs.

CODE:

from tkinter import *  
def reset():      
    input_field.delete(0, END)  
    output_field.delete(0, END)  
    input_value.set(SELECTIONS[0])  
    output_value.set(SELECTIONS[0])  
    input_field.focus_set()    
# defining the convert function  
def convert():  
    inputVal = float(input_field.get())  
    input_unit = input_value.get()  
    output_unit = output_value.get()     
    conversion_factors = [input_unit in length_units and output_unit in length_units,  
    input_unit in weight_units and output_unit in weight_units,  
    input_unit in temperature_units and output_unit in temperature_units,  
    input_unit in area_units and output_unit in area_units,  
    input_unit in volume_units and output_unit in volume_units]  
  
    if any(conversion_factors): 
        if input_unit == "celsius" and output_unit == "fahrenheit":  
            output_field.delete(0, END)  
            output_field.insert(0, (inputVal * 1.8) + 32)  
        elif input_unit == "fahrenheit" and output_unit == "celsius":  
            output_field.delete(0, END)  
            output_field.insert(0, (inputVal - 32) * (5/9))  
        else:  
            output_field.delete(0, END)  
            output_field.insert(0, round(inputVal * unitDict[input_unit] / unitDict[output_unit], 5))    
    else:   
        output_field.delete(0, END)  
        output_field.insert(0, "ERROR")    
if __name__ == "__main__":      
    unitDict = {  
        "millimeter" : 0.001,  
        "centimeter" : 0.01,  
        "meter" : 1.0,  
        "kilometer" : 1000.0,  
        "foot" : 0.3048,  
        "mile" : 1609.344,  
        "yard" : 0.9144,  
        "inch" : 0.0254,  
        "square meter" : 1.0,  
        "square kilometer" : 1000000.0,  
        "square centimeter" : 0.0001,  
        "square millimeter" : 0.000001,  
        "are" : 100.0,  
        "hectare" : 10000.0,  
        "acre" : 4046.856,  
        "square mile" : 2590000.0,  
        "square foot" : 0.0929,  
        "cubic meter" : 1000.0,  
        "cubic centimeter" : 0.001,  
        "litre" :  1.0,  
        "millilitre" : 0.001,  
        "gallon" : 3.785,  
        "gram" : 1.0,  
        "kilogram" : 1000.0,  
        "milligram" : 0.001,  
        "quintal" : 100000.0,  
        "ton" : 1000000.0,  
        "pound" : 453.592,  
        "ounce" : 28.3495  
    }    
    length_units = [  
        "millimeter", "centimeter", "meter", "kilometer", "foot", "mile", "yard", "inch"  
        ]  
    temperature_units = [  
        "celsius", "fahrenheit"  
    ]  
    area_units = [  
        "square meter", "square kilometer", "square centimeter", "square millimeter",  
        "are", "hectare", "acre", "square mile", "square foot"  
        ]  
    volume_units = [  
        "cubic meter", "cubic centimeter", "litre", "millilitre", "gallon"     
    ]  
    weight_units = [  
        "gram", "kilogram", "milligram", "quintal", "ton", "pound", "ounce"  
    ]     
    SELECTIONS = [  
        "Select Unit",  
        "millimeter",  
        "centimeter",  
        "meter",  
        "kilometer",  
        "foot",  
        "mile",  
        "yard",  
        "inch",  
        "celsius",  
        "fahrenheit"  
        "square meter",  
        "square kilometer",  
        "square centimeter",  
        "square millimeter",  
        "are",  
        "hectare",  
        "acre",  
        "square mile",  
        "square foot"  
        "cubic meter",  
        "cubic centimeter",  
        "litre",  
        "millilitre",  
        "gallon"     
        "gram",  
        "kilogram",  
        "milligram",  
        "quintal",  
        "ton",  
        "pound",  
        "ounce"  
    ]  
  
    guiWindow = Tk()  
    guiWindow.title("Unit Converter - JAVATPOINT")  
    guiWindow.geometry("500x500+500+250") 
    guiWindow.resizable(0, 0)  
    guiWindow.configure(bg = "#16a085")  
  
    header_frame = Frame(guiWindow, bg = "#16a085")  
    body_frame = Frame(guiWindow, bg = "#16a085")  
  
    header_frame.pack(expand = True, fill = "both")  
    body_frame.pack(expand = True, fill = "both")  
    
    header_label = Label(  
        header_frame,  
        text = "STANDARD UNIT CONVERTER",  
        font = ("arial black", 16),  
        bg = "#16a085",  
        fg = "#e8f6f3"  
    )  
    header_label.pack(expand = True, fill = "both")    
    input_value = StringVar()  
    output_value = StringVar()  
     
    input_value.set(SELECTIONS[0])  
    output_value.set(SELECTIONS[0])   
    input_label = Label(  
        body_frame,  
        text = "From:",  
        bg = "#16a085",  
        fg = "#d0ece7"  
    )  
    output_label = Label(  
        body_frame,  
        text = "To:",  
        bg = "#16a085",  
        fg = "#d0ece7"  
    )  
  
    input_label.grid(row = 1, column = 1, padx = 50, pady = 20, sticky = W)  
    output_label.grid(row = 2, column = 1, padx = 50, pady = 20, sticky = W)  
  
    input_field = Entry(  
        body_frame,  
        bg = "#e8f8f5"  
    )   
    output_field = Entry(  
        body_frame,  
        bg = "#e8f8f5"  
    )     
    input_field.grid(row = 1, column = 2)  
    output_field.grid(row = 2, column = 2)    
    input_menu = OptionMenu(  
        body_frame,  
        input_value,  
        *SELECTIONS  
    )  
    output_menu = OptionMenu(  
        body_frame,  
        output_value,  
        *SELECTIONS  
    )    
    input_menu.grid(row = 1, column = 3, padx = 20)  
    output_menu.grid(row = 2, column = 3, padx = 20)  
  
    convert_button = Button(  
        body_frame,  
        text = "CONVERT",  
        bg = "#0b5345",  
        fg = "#ffffff",  
        command = convert  
    )  
    reset_button = Button(  
        body_frame,  
        text = "RESET",  
        bg = "#f7dc6f",  
        fg = "#000000",  
        command = reset  
    )      
    convert_button.grid(row = 3, column = 2)  
    reset_button.grid(row = 3, column = 3)    
    guiWindow.mainloop()  

OUTPUT:

Weight Conversion GUI using Tkinter in Python

Related Topics

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.

Python Marshmallow

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python PyMOTW

The cmd module comprises one class of the general public, Cmd, meant for command processors such interactive shells and other command interpreters as a foundation class. It utilises readline as...

3 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

What is Ipython shell?

IPython is a command shell for computing in multiple programming languages. IPython was developed for python language by Fernando Perez in 2001 as a well-equipped python interpreter that offers shell...

3 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.