erynur

Resistor calculation

Apr 4th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. """
  2. Code by: erynur
  3.  
  4. Feel free to use this code for personal or educational purpose
  5. """
  6.  
  7. import base64
  8. from urllib.request import urlopen
  9. try:
  10.     from tkinter import Tk, Button, Label, Frame, Menu, Entry, Radiobutton, StringVar, PhotoImage, messagebox
  11.  
  12. except ImportError:
  13.     from Tkinter import Tk, Button, Label, Frame, Menu, Entry, Radiobutton, StringVar, PhotoImage
  14.  
  15.  
  16. value_colors = {
  17.     'black': 0,
  18.     'brown': 1,
  19.     'red': 2,
  20.     'orange': 3,
  21.     'yellow': 4,
  22.     'green': 5,
  23.     'blue': 6,
  24.     'violet': 7,
  25.     'grey': 8,
  26.     'white': 9
  27.     }
  28.  
  29. multiplier_colors = {
  30.     'black': (1, ''),
  31.     'brown': (10, ''),
  32.     'red': (100, ''),
  33.     'orange': (1, 'K'),
  34.     'yellow': (10, 'K'),
  35.     'green': (100, 'K'),
  36.     'blue': (1, 'M'),
  37.     'violet': (10, 'M'),
  38.     'grey': (100, 'M'),
  39.     'white': (1, 'G'),
  40.     'gold': (0.1, ''),
  41.     'silver': (0.01, '')
  42. }
  43.  
  44. tolerance_colors = {
  45.     'brown': '1%',
  46.     'red': '2%',
  47.     'green': '0.5%',
  48.     'blue': '0.25%',
  49.     'violet': '0.1%',
  50.     'grey': '0.05%',
  51.     'gold': '5%',
  52.     'silver': '10%'
  53. }
  54.  
  55.  
  56. def quit_application():
  57.     quit()
  58.  
  59.  
  60. def activate():
  61.     entry_ring_3.config(state='normal')
  62.  
  63.  
  64. def deactivate():
  65.     entry_ring_3.config(state='disabled')
  66.  
  67.  
  68. def calculate(n):
  69.     try:
  70.         ring_1 = entry_ring_1.get().lower()
  71.         ring_2 = entry_ring_2.get().lower()
  72.         ring_3 = entry_ring_3.get().lower()
  73.         multiplier = multiplier_colors[entry_multiplier.get().lower()]
  74.         tolerance = tolerance_colors[entry_tolerance.get().lower()]
  75.         if n == '5':
  76.             res = str(value_colors[ring_1]) + str(value_colors[ring_2]) + str(value_colors[ring_3])
  77.         else:
  78.             res = str(value_colors[ring_1]) + str(value_colors[ring_2])
  79.         res = str(int(res) * multiplier[0]) + multiplier[1] + ' +/- ' + tolerance
  80.         result.set(res)
  81.     except KeyError:
  82.         messagebox.showinfo(title='Wrong input', message='Wrong input')
  83.         root.destroy()
  84.  
  85.  
  86. root = Tk()
  87.  
  88.  
  89. # Menubar to Quit the game
  90. menuBar = Menu(root)
  91. root.config(menu=menuBar)
  92.  
  93. subMenuFile = Menu(menuBar, tearoff=0)
  94. menuBar.add_cascade(label='File', menu=subMenuFile)
  95. subMenuFile.add_command(label='Quit', command=quit_application)
  96.  
  97. image_byt = urlopen('https://www.digikey.com/-/media/Images/Marketing/Resources/Calculators/resistor-color-chart.png?ts=e802ab48-1ea0-4745-babf-9a21accec5c2&la=en-US').read()
  98. image_b64 = base64.encodebytes(image_byt)
  99.  
  100. resistor_color = PhotoImage(data=image_b64)
  101.  
  102. image_label = Label(root, image=resistor_color)
  103. image_label.grid(row=0, column=0, columnspan=5)
  104.  
  105. label_ring_1 = Label(root, text='Ringfarbe 1:')
  106. label_ring_1.grid(row=2, column=0)
  107. label_ring_2 = Label(root, text='Ringfarbe 2:')
  108. label_ring_2.grid(row=2, column=1)
  109. label_ring_3 = Label(root, text='Ringfarbe 3:')
  110. label_ring_3.grid(row=2, column=2)
  111. label_multiplier = Label(root, text='Multiplier:')
  112. label_multiplier.grid(row=2, column=3)
  113. label_tolerance = Label(root, text='Tolerance:')
  114. label_tolerance.grid(row=2, column=4)
  115.  
  116. label_result = Label(root, text='Result')
  117. label_result.grid(row=5, column=1)
  118.  
  119. result = StringVar()
  120. result.set('Ergebnis')
  121. show_result = Label(root, textvariable=result)
  122. show_result.grid(row=5, column=4)
  123.  
  124. radiobutton_value = StringVar()
  125.  
  126. radiobutton_4_rings = Radiobutton(
  127.     root,
  128.     text='4 Rings',
  129.     value=4,
  130.     variable=radiobutton_value,
  131.     indicatoron=0,
  132.     command=deactivate
  133. )
  134. radiobutton_4_rings.grid(row=1, column=1)
  135. radiobutton_4_rings.select()
  136.  
  137. radiobutton_5_rings = Radiobutton(
  138.     root,
  139.     text='5 Rings',
  140.     value=5,
  141.     variable=radiobutton_value,
  142.     indicatoron=0,
  143.     command=activate
  144. )
  145. radiobutton_5_rings.grid(row=1, column=3)
  146.  
  147. entry_ring_1 = Entry(
  148.     root,
  149.     justify='center'
  150. )
  151. entry_ring_1.grid(row=3, column=0)
  152. entry_ring_2 = Entry(root, justify='center')
  153. entry_ring_2.grid(row=3, column=1)
  154. entry_ring_3 = Entry(
  155.     root,
  156.     justify='center',
  157.     state='disabled',
  158.     disabledbackground='darkgrey'
  159. )
  160. entry_ring_3.grid(row=3, column=2)
  161. entry_multiplier = Entry(root, justify='center')
  162. entry_multiplier.grid(row=3, column=3)
  163. entry_tolerance = Entry(root, justify='center')
  164. entry_tolerance.grid(row=3, column=4)
  165.  
  166. calculate_button = Button(
  167.     root,
  168.     text='Show',
  169.     command=lambda: calculate(radiobutton_value.get()),
  170.     state='active'
  171. )
  172. calculate_button.grid(row=4, column=2)
  173.  
  174. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment