How to Create a Resistor Color Code Calculator with GUI using Tkinter in Python

Posted

in

by

This code is a Resistor Color Code Calculator. A resistor is an electronic component used in circuits to control the flow of electricity. The colors of the bands on the resistor indicate the resistance value of the resistor. This code allows you to select the color of the four bands on the resistor and then calculate the resistance value of the resistor.

The code uses a graphical user interface (GUI) built using a library called tkinter. The GUI consists of labels, dropdown menus, a button, and a link. The dropdown menus allow you to select the colors of the four bands on the resistor. The button, when pressed, calculates the resistance value of the resistor and displays it on the screen. The link is a hyperlink that takes you to a website.

The code defines two dictionaries that hold the color codes and tolerance values for resistors. The calculate_resistance function uses the selected colors to calculate the resistance value of the resistor. The open_website function opens a website when the link is clicked.

Code

import tkinter as tk
import webbrowser

# Define the color code values
color_codes = {
    "black": {"value": 0, "color": "#000000"},
    "brown": {"value": 1, "color": "#964B00"},
    "red": {"value": 2, "color": "#FF0000"},
    "orange": {"value": 3, "color": "#FFA500"},
    "yellow": {"value": 4, "color": "#FFFF00"},
    "green": {"value": 5, "color": "#008000"},
    "blue": {"value": 6, "color": "#0000FF"},
    "violet": {"value": 7, "color": "#8B00FF"},
    "gray": {"value": 8, "color": "#808080"},
    "white": {"value": 9, "color": "#FFFFFF"}
}

# Define the tolerance values
tolerances = {
    "brown": {"value": 1, "color": "#964B00"},
    "gold": {"value": 5, "color": "#FFD700"},
    "silver": {"value": 10, "color": "#C0C0C0"}
}

# Define the GUI
root = tk.Tk()
root.title("Resistor Color Code Calculator")

# Define the functions
def calculate_resistance(band1, band2, band3, band4):
    resistance_value = (color_codes[band1]["value"] * 10 + color_codes[band2]["value"]) * (10 ** color_codes[band3]["value"])
    if band4 != "none":
        tolerance_value = tolerances[band4]["value"]
        return f"Resistance value: {resistance_value} ohms, Tolerance: ±{tolerance_value}%"
    else:
        return f"Resistance value: {resistance_value} ohms"

def open_website():
    webbrowser.open_new("http://www.exasub.com")

# Define the GUI elements
label1 = tk.Label(root, text="Select the color of the first band:")
label1.grid(row=0, column=0)

band1_var = tk.StringVar(root)
band1_var.set("black")

band1_color_label = tk.Label(root, bg=color_codes[band1_var.get()]["color"], width=10)
band1_color_label.grid(row=0, column=1)

band1_dropdown = tk.OptionMenu(root, band1_var, *color_codes.keys(), command=lambda _: band1_color_label.config(bg=color_codes[band1_var.get()]["color"]))
band1_dropdown.grid(row=0, column=2)

label2 = tk.Label(root, text="Select the color of the second band:")
label2.grid(row=1, column=0)

band2_var = tk.StringVar(root)
band2_var.set("black")

band2_color_label = tk.Label(root, bg=color_codes[band2_var.get()]["color"], width=10)
band2_color_label.grid(row=1, column=1)

band2_dropdown = tk.OptionMenu(root, band2_var, *color_codes.keys(), command=lambda _: band2_color_label.config(bg=color_codes[band2_var.get()]["color"]))
band2_dropdown.grid(row=1, column=2)

label3 = tk.Label(root, text="Select the color of the third band:")
label3.grid(row=2, column=0)

band3_var = tk.StringVar(root)
band3_var.set("black")

band3_color_label = tk.Label(root, bg=color_codes[band3_var.get()]["color"], width=10)
band3_color_label.grid(row=2, column=1)

band3_dropdown = tk.OptionMenu(root, band3_var, *color_codes.keys(), command=lambda _: band3_color_label.config(bg=color_codes[band3_var.get()]["color"]))
band3_dropdown.grid(row=2, column=2)

label4 = tk.Label(root, text="Select the color of the fourth band (optional):")
label4.grid(row=3, column=0)

band4_var = tk.StringVar(root)
band4_var.set("gold")

band4_color_label = tk.Label(root, bg=tolerances[band4_var.get()]["color"], width=10)
band4_color_label.grid(row=3, column=1)

band4_dropdown = tk.OptionMenu(root, band4_var, *(["none"] + list(tolerances.keys())), command=lambda _: band4_color_label.config(bg=tolerances[band4_var.get()]["color"]))
band4_dropdown.grid(row=3, column=2)

calculate_button = tk.Button(root, text="Calculate", command=lambda: result_label.config(text=calculate_resistance(band1_var.get(), band2_var.get(), band3_var.get(), band4_var.get())))
calculate_button.grid(row=4, column=1)

result_label = tk.Label(root, text="", font=("Arial", 14))
result_label.grid(row=5, column=0, columnspan=3)

link_label = tk.Label(root, text="exasub.com", font=("Arial", 14), fg="blue", cursor="hand2")
link_label.grid(row=6, column=0, pady=10)
link_label.bind("<Button-1>", lambda event: open_website())

root.mainloop()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *