×

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

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Division in Python

In this tutorial, we will understand what mathematical operation division is and how is it performed in Python Programming language. We will also focus on the operators being used in...

3 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Python program to add two number

Python program to add two number This program will add the two numbers and display their sum on the screen. Example: Input: Number1 = 20        Number2 = 30   Output: Sum =...

2 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.