Weight Conversion GUI using Tkinter

Graphical User Interfaces (GUIs) are an integral part of modern software applications, providing an interactive and user-friendly experience. In this tutorial, we will explore how to create a Weight Conversion GUI using Tkinter, a popular Python library for creating GUI applications.

Our Weight Conversion GUI will allow users to easily convert weights between different units such as kilograms (kg), pounds (lb), and ounces (oz).

Importing Required Modules

We'll begin by importing the necessary modules. Tkinter provides the core functionality for building GUIs ?

import tkinter as tk
from tkinter import ttk

Here, we import the tkinter module as tk and the ttk module, which provides themed widgets.

Creating the Complete Weight Converter

Let's create the complete weight conversion application with all functionality ?

import tkinter as tk
from tkinter import ttk

def convert_weight():
    try:
        kg_value = float(entry_kg.get())
        lb_value = kg_value * 2.20462
        oz_value = kg_value * 35.274

        entry_lb.delete(0, tk.END)
        entry_lb.insert(0, round(lb_value, 2))

        entry_oz.delete(0, tk.END)
        entry_oz.insert(0, round(oz_value, 2))
    except ValueError:
        # Handle invalid input - clear other fields
        entry_lb.delete(0, tk.END)
        entry_oz.delete(0, tk.END)

def create_gui():
    global entry_kg, entry_lb, entry_oz
    
    # Create the main window
    window = tk.Tk()
    window.title("Weight Conversion")
    window.geometry("300x200")
    window.resizable(False, False)

    # Create the labels
    label_kg = ttk.Label(window, text="Kilograms:")
    label_kg.grid(column=0, row=0, padx=10, pady=10, sticky="w")

    label_lb = ttk.Label(window, text="Pounds:")
    label_lb.grid(column=0, row=1, padx=10, pady=10, sticky="w")

    label_oz = ttk.Label(window, text="Ounces:")
    label_oz.grid(column=0, row=2, padx=10, pady=10, sticky="w")

    # Create the entry fields
    entry_kg = ttk.Entry(window, width=15)
    entry_kg.grid(column=1, row=0, padx=10, pady=10)

    entry_lb = ttk.Entry(window, width=15, state="readonly")
    entry_lb.grid(column=1, row=1, padx=10, pady=10)

    entry_oz = ttk.Entry(window, width=15, state="readonly")
    entry_oz.grid(column=1, row=2, padx=10, pady=10)

    # Create the conversion button
    button_convert = ttk.Button(window, text="Convert", command=convert_weight)
    button_convert.grid(column=0, row=3, columnspan=2, padx=10, pady=20)

    # Bind Enter key to conversion
    window.bind('<Return>', lambda event: convert_weight())
    
    # Focus on input field
    entry_kg.focus()

    # Start the GUI event loop
    window.mainloop()

# Run the application
if __name__ == "__main__":
    create_gui()

How It Works

The application consists of several key components:

  • Input Field: Users enter weight in kilograms
  • Output Fields: Display converted pounds and ounces (readonly)
  • Convert Button: Triggers the conversion calculation
  • Error Handling: Manages invalid input gracefully

Conversion Logic

The convert_weight() function performs the following steps:

  1. Retrieves the kilogram value from the input field
  2. Converts to pounds using factor 2.20462
  3. Converts to ounces using factor 35.274
  4. Updates the display fields with rounded results
  5. Handles ValueError for invalid input

Key Features

  • Readonly Output Fields: Prevents accidental editing of results
  • Enter Key Support: Press Enter to convert without clicking button
  • Fixed Window Size: Maintains consistent layout
  • Input Focus: Cursor automatically in input field
  • Error Handling: Clears output fields for invalid input

Sample Usage

Here are some example conversions you can test:

Input (kg) Output (lbs) Output (oz)
5.0 11.02 176.37
2.5 5.51 88.18
10.0 22.05 352.74

Running the Application

Save the code as weight_converter.py and run it:

python weight_converter.py

The GUI window will appear, allowing you to enter weights and see instant conversions.

Conclusion

This Weight Conversion GUI demonstrates fundamental Tkinter concepts including window creation, widget placement, event handling, and user input validation. The application provides a clean, functional interface for converting weights from kilograms to pounds and ounces with proper error handling.

Updated on: 2026-03-27T12:37:55+05:30

574 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements