Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a Weight Conversion GUI using Tkinter is a good exercise to understand the basics of GUI development. Here's a simple tutorial to build a weight conversion application that converts between kilograms, grams, pounds, and ounces.
Start by importing the required modules and setting up the main window:
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Weight Converter") root.geometry("400x300")
def convert_weight(): # Get the input value and the conversion units try: weight = float(weight_entry.get()) from_unit = from_combobox.get() to_unit = to_combobox.get() # Conversion to base unit (kilogram) if from_unit == "Pounds": weight = weight / 2.20462 elif from_unit == "Ounces": weight = weight / 35.274 elif from_unit == "Grams": weight = weight / 1000 # Conversion from base unit to target unit if to_unit == "Pounds": weight = weight * 2.20462 elif to_unit == "Ounces": weight = weight * 35.274 elif to_unit == "Grams": weight = weight * 1000 # Display the result result_label.config(text=f"Converted Weight: {weight:.2f} {to_unit}") except ValueError: result_label.config(text="Invalid input. Please enter a number.")
Weight Entry and Labels:
frame_input = ttk.Frame(root) frame_input.pack(pady=20) label = ttk.Label(frame_input, text="Enter Weight:") label.grid(row=0, column=0, padx=10) weight_entry = ttk.Entry(frame_input) weight_entry.grid(row=0, column=1, padx=10) weight_entry.insert(0, "0")
Dropdowns for Unit Selection:
units = ["Kilograms", "Grams", "Pounds", "Ounces"] from_combobox = ttk.Combobox(frame_input, values=units, width=10) from_combobox.grid(row=1, column=0, padx=10, pady=10) from_combobox.set("Kilograms") to_label = ttk.Label(frame_input, text="to") to_label.grid(row=1, column=1) to_combobox = ttk.Combobox(frame_input, values=units, width=10) to_combobox.grid(row=1, column=2, padx=10, pady=10) to_combobox.set("Pounds")
Button for Conversion:
convert_btn = ttk.Button(root, text="Convert", command=convert_weight) convert_btn.pack(pady=20)
Label for Displaying Result:
result_label = ttk.Label(root, text="Converted Weight: -") result_label.pack(pady=20)
root.mainloop()
When you piece all the code snippets together and run the script, you'll have a functional Weight Conversion GUI application using Tkinter. The user can input a weight, select the input and output units, and get the converted weight.
import tkinter as tk def convert_weight(): try: # Get input weight from the entry widget input_weight = float(entry.get()) # Conversion rates (example: kg to pounds) kg_to_pounds = 2.20462 result = input_weight * kg_to_pounds # Update the result label result_label.config(text=f"{input_weight} kg is {result:.2f} pounds") except ValueError: result_label.config(text="Invalid input, please enter a number") # Create main window root = tk.Tk() root.title("Weight Converter") # Entry widget for weight input entry = tk.Entry(root, width=30) entry.grid(row=0, column=0, padx=10, pady=10) # Convert button convert_button = tk.Button(root, text="Convert", command=convert_weight) convert_button.grid(row=0, column=1, padx=5, pady=10) # Result label result_label = tk.Label(root, text="") result_label.grid(row=1, column=0, columnspan=2, padx=10, pady=10) # Start the Tkinter event loop root.mainloop()
This is similar to the first example.
This is also similar to the first example.
The first example covers the basics of building a simple weight conversion app.
import tkinter as tk def convert_weight(): try: input_weight = float(entry.get()) selected_unit = unit_var.get() # Conversion rates for different units unit_conversion = { "Kilograms to Pounds": 2.20462, "Pounds to Kilograms": 0.453592, # Add more units as needed } result = input_weight * unit_conversion[selected_unit] result_label.config(text=f"{input_weight} {selected_unit.split(' to ')[0]} is {result:.2f} {selected_unit.split(' to ')[1]}") except ValueError: result_label.config(text="Invalid input, please enter a number") # Create main window root = tk.Tk() root.title("Weight Converter") # Entry widget for weight input entry = tk.Entry(root, width=30) entry.grid(row=0, column=0, padx=10, pady=10) # Dropdown menu for selecting weight units units = ["Kilograms to Pounds", "Pounds to Kilograms"] unit_var = tk.StringVar(root) unit_var.set(units[0]) unit_dropdown = tk.OptionMenu(root, unit_var, *units) unit_dropdown.grid(row=0, column=1, padx=5, pady=10) # Convert button convert_button = tk.Button(root, text="Convert", command=convert_weight) convert_button.grid(row=0, column=2, padx=5, pady=10) # Result label result_label = tk.Label(root, text="") result_label.grid(row=1, column=0, columnspan=3, padx=10, pady=10) # Start the Tkinter event loop root.mainloop()
This is similar to the fifth example.
The first example serves as a guide for creating a GUI weight converter.
This can be considered as a project example with the first code snippet.