image.png (22.26 KB, 下载次数: 0)
下载附件
网页版
2024-9-24 19:20 上传
网页版代码,感谢反馈的BUG,在win7平台的IE11下发现问题是因为IE不支持 Array.from(),这是修正后的代码,[HTML] 纯文本查看 复制代码
色环电阻计算器
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.color-box {
display: inline-block;
width: 50px;
height: 50px;
margin: 5px;
border: 1px solid #000;
}
#colorInputs {
margin-bottom: 20px;
}
色环电阻计算器
选择色环数量:
四色环
五色环
六色环
选择颜色:
计算
计算结果:
image.png (32.14 KB, 下载次数: 0)
下载附件
2024-9-24 19:21 上传
PY代码:[Python] 纯文本查看 复制代码 import tkinter as tk
from tkinter import ttk
def get_color_code():
return {
"黑": 0, "棕": 1, "红": 2, "橙": 3,
"黄": 4, "绿": 5, "蓝": 6, "紫": 7,
"灰": 8, "白": 9, "金": 0, "银": 0
}
def get_tolerance_map():
return {
"黑": 0, "棕": 1, "红": 2, "橙": 3,
"黄": 4, "绿": 0.5, "蓝": 0.25, "紫": 0.1,
"灰": 0.05, "白": 0, "金": 5, "银": 10
}
def rgb_to_hex(color):
"""Convert color name to hex value."""
color_codes = {
"黑": "#000000", "棕": "#964B00", "红": "#FF0000",
"橙": "#FFA500", "黄": "#FFFF00", "绿": "#008000",
"蓝": "#0000FF", "紫": "#800080", "灰": "#808080",
"白": "#FFFFFF", "金": "#FFD700", "银": "#C0C0C0"
}
return color_codes.get(color, "#FFFFFF")
def calculate_resistor(num_bands, selected_colors):
color_map = get_color_code()
tolerance_map = get_tolerance_map()
# Convert colors to value codes
color_codes = [color_map[color] for color in selected_colors]
# Calculate resistance value
if num_bands == 4:
resistor_value = (color_codes[0] * 10 + color_codes[1]) * (10 ** color_codes[2])
else:
resistor_value = (color_codes[0] * 100 + color_codes[1] * 10 + color_codes[2]) * (10 ** color_codes[3])
# Get the tolerance from the last color
tolerance_color = selected_colors[-1]
tolerance_percentage = tolerance_map[tolerance_color]
# Calculate tolerance range
tolerance_value = resistor_value * (tolerance_percentage / 100)
min_value = round(resistor_value - tolerance_value, 2)
max_value = round(resistor_value + tolerance_value, 2)
return resistor_value, tolerance_percentage, min_value, max_value
def on_calculate():
num_bands = int(bands_combobox.get())
selected_colors = [color_vars.get() for i in range(num_bands)]
resistor_value, tolerance_percentage, min_value, max_value = calculate_resistor(num_bands, selected_colors)
# Update result label
result_label.config(text=f"电阻值: {resistor_value} 欧姆 (±{tolerance_percentage}%), 范围: {min_value} 欧姆 - {max_value} 欧姆")
# Clear color display frame
for widget in color_display_frame.winfo_children():
widget.destroy()
# Display color squares
for color in selected_colors:
hex_color = rgb_to_hex(color)
color_box = tk.Label(color_display_frame, bg=hex_color, width=3, height=2)
color_box.pack(side=tk.LEFT, padx=5)
def on_reset():
bands_combobox.set("4")
for color_var in color_vars:
color_var.set("黑") # Default to black
result_label.config(text="")
# Clear color display frame
for widget in color_display_frame.winfo_children():
widget.destroy()
def update_color_comboboxes(event):
num_bands = int(bands_combobox.get())
for i in range(6):
if i >", update_color_comboboxes)
# Color selection
colors = ["黑", "棕", "红", "橙", "黄", "绿", "蓝", "紫", "灰", "白", "金", "银"]
color_vars = []
color_comboboxes = []
for i in range(6): # Allow up to 6 colors
color_var = tk.StringVar(value="黑") # Default to black
color_vars.append(color_var)
label = tk.Label(root, text=f"选择第 {i + 1} 条颜色:")
label.pack()
color_combobox = ttk.Combobox(root, textvariable=color_var, values=colors)
color_combobox.set("黑") # Default to black
color_combobox.pack()
color_comboboxes.append(color_combobox)
# Hide excess color selection boxes
update_color_comboboxes(None)
# Calculate and reset buttons
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
calculate_button = tk.Button(button_frame, text="计算", command=on_calculate)
calculate_button.pack(side=tk.LEFT, padx=10)
reset_button = tk.Button(button_frame, text="复位", command=on_reset)
reset_button.pack(side=tk.LEFT, padx=10)
# Result display
result_label = tk.Label(root, text="")
result_label.pack(pady=10)
# Color display frame
color_display_frame = tk.Frame(root)
color_display_frame.pack(pady=10)
# Start main loop
root.mainloop()