#!/usr/bin/python3 # =================================================================== # Calculate and display a resistor's value # =================================================================== # This code uses the PySimpleGUI module. It is also currently # incomplete. The following needs to be added: # c. Error messages when appropriate. # d. How to handle resistors with 3,4,5, or 6 bands? # Perhaps display a diagram? Let the user choose how many bands? # Only handle 3 bands, tolerance, and multiplier resisters? # =================================================================== import PySimpleGUI as sg band1_colors = [ 'None','Brown','Red','Orange','Yellow','Green', 'Blue','Violet','Grey','White' ] band2_colors = [ 'None','Black','Brown','Red','Orange','Yellow', 'Green','Blue','Violet','Grey','White' ] band3_colors = [ 'None','Black','Brown','Red','Orange','Yellow', 'Green','Blue','Violet','Grey','White' ] multiplier_colors = [ 'None','Brown','Red','Orange','Yellow','Green', 'Blue','Violet','Grey','White','Gold','Silver' ] tolerance_colors = [ 'None','Brown','Red','Green','Blue','Violet', 'Grey','Gold','Silver' ] color_values = { 'None':0,'Black':0,'Brown':1,'Red':2,'Orange':3, 'Yellow':4,'Green':5,'Blue':6,'Violet':7, 'Grey':8,'White':9 } multiplier = { 'None':1,'Black':1,'Brown':10,'Red':100, 'Orange':1000,'Yellow':10000,'Green':100000, 'Blue':1000000,'Violet':10000000, 'Grey':100000000,'White':1000000000, 'Gold':0.1,'Silver':0.01 } tolerance = { 'None':'unk','Brown':'+/1 1%','Red':'+/- 2%', 'Green':'+/- 0.5%','Blue':'+/- 0.25%', 'Violet':'+/- 0.1%','Grey':'+/- 0.05%', 'Gold':'+/- 5%','Silver':'+/- 10%' } font1 = ('Arial',14) font2 = ('Aerial',12) layout = [ [sg.Text('1st Band',font=font2), sg.Text('2dn Band',font=font2), sg.Text('3rd Band',font=font2), sg.Text('Multiplier',font=font2), sg.Text('Tolerance',font=font2)], [sg.Combo(band1_colors,default_value='None', font=font1,key='band1'), sg.Combo(band2_colors,default_value='None', font=font1,key='band2'), sg.Combo(band3_colors,default_value='None', font=font1,key='band3'), sg.Combo(multiplier_colors,default_value='None', font=font1,key='multi'), sg.Combo(tolerance_colors,default_value='None', font=font1,key='toler')], [sg.Button('Exit',font=font2), sg.Button('Calculate',font=font2), sg.Button('Reset',font=font2)], [sg.Text(text_color='blue',background_color='Yellow', size=(80,8),font=font1,key='-MSG-')]] # ------------------------------------------------------------------- # ---- calculate resistor's value # ------------------------------------------------------------------- def calculate(values): # ---- get resistor values b1 = values['band1'] b2 = values['band2'] b3 = values['band3'] mu = values['multi'] to = values['toler'] # ---- test for valid resistor values val = color_values[b1] val = val * 10 + color_values[b2] val = val * 10 + color_values[b3] val = val * multiplier[mu] return (val,tolerance[to]) # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- # ---- Create the window win = sg.Window("Resistor Value",layout) # ---- Create an event loop while True: event, values = win.read() # ---- exit program if event == "Exit" or event == sg.WIN_CLOSED: break # ---- reset if event == "Reset": win['band1'].update('None') win['band2'].update('None') win['band3'].update('None') win['multi'].update('None') win['toler'].update('None') win['-MSG-'].update('') continue # ---- calculate and display resistor's value if event == "Calculate": (val,tol) = calculate(values) win['-MSG-'].update(f'{val}\u03A9 {tol}') win.close()