Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Code by: erynur
- Contact: [email protected]
- Feel free to use this code for personal or educational purpose
- """
- import base64
- from urllib.request import urlopen
- try:
- from tkinter import Tk, Button, Label, Frame, Menu, Entry, Radiobutton, StringVar, PhotoImage, messagebox
- except ImportError:
- from Tkinter import Tk, Button, Label, Frame, Menu, Entry, Radiobutton, StringVar, PhotoImage
- value_colors = {
- 'black': 0,
- 'brown': 1,
- 'red': 2,
- 'orange': 3,
- 'yellow': 4,
- 'green': 5,
- 'blue': 6,
- 'violet': 7,
- 'grey': 8,
- 'white': 9
- }
- multiplier_colors = {
- 'black': (1, ''),
- 'brown': (10, ''),
- 'red': (100, ''),
- 'orange': (1, 'K'),
- 'yellow': (10, 'K'),
- 'green': (100, 'K'),
- 'blue': (1, 'M'),
- 'violet': (10, 'M'),
- 'grey': (100, 'M'),
- 'white': (1, 'G'),
- 'gold': (0.1, ''),
- 'silver': (0.01, '')
- }
- tolerance_colors = {
- 'brown': '1%',
- 'red': '2%',
- 'green': '0.5%',
- 'blue': '0.25%',
- 'violet': '0.1%',
- 'grey': '0.05%',
- 'gold': '5%',
- 'silver': '10%'
- }
- def quit_application():
- quit()
- def activate():
- entry_ring_3.config(state='normal')
- def deactivate():
- entry_ring_3.config(state='disabled')
- def calculate(n):
- try:
- ring_1 = entry_ring_1.get().lower()
- ring_2 = entry_ring_2.get().lower()
- ring_3 = entry_ring_3.get().lower()
- multiplier = multiplier_colors[entry_multiplier.get().lower()]
- tolerance = tolerance_colors[entry_tolerance.get().lower()]
- if n == '5':
- res = str(value_colors[ring_1]) + str(value_colors[ring_2]) + str(value_colors[ring_3])
- else:
- res = str(value_colors[ring_1]) + str(value_colors[ring_2])
- res = str(int(res) * multiplier[0]) + multiplier[1] + ' +/- ' + tolerance
- result.set(res)
- except KeyError:
- messagebox.showinfo(title='Wrong input', message='Wrong input')
- root.destroy()
- root = Tk()
- # Menubar to Quit the game
- menuBar = Menu(root)
- root.config(menu=menuBar)
- subMenuFile = Menu(menuBar, tearoff=0)
- menuBar.add_cascade(label='File', menu=subMenuFile)
- subMenuFile.add_command(label='Quit', command=quit_application)
- 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()
- image_b64 = base64.encodebytes(image_byt)
- resistor_color = PhotoImage(data=image_b64)
- image_label = Label(root, image=resistor_color)
- image_label.grid(row=0, column=0, columnspan=5)
- label_ring_1 = Label(root, text='Ringfarbe 1:')
- label_ring_1.grid(row=2, column=0)
- label_ring_2 = Label(root, text='Ringfarbe 2:')
- label_ring_2.grid(row=2, column=1)
- label_ring_3 = Label(root, text='Ringfarbe 3:')
- label_ring_3.grid(row=2, column=2)
- label_multiplier = Label(root, text='Multiplier:')
- label_multiplier.grid(row=2, column=3)
- label_tolerance = Label(root, text='Tolerance:')
- label_tolerance.grid(row=2, column=4)
- label_result = Label(root, text='Result')
- label_result.grid(row=5, column=1)
- result = StringVar()
- result.set('Ergebnis')
- show_result = Label(root, textvariable=result)
- show_result.grid(row=5, column=4)
- radiobutton_value = StringVar()
- radiobutton_4_rings = Radiobutton(
- root,
- text='4 Rings',
- value=4,
- variable=radiobutton_value,
- indicatoron=0,
- command=deactivate
- )
- radiobutton_4_rings.grid(row=1, column=1)
- radiobutton_4_rings.select()
- radiobutton_5_rings = Radiobutton(
- root,
- text='5 Rings',
- value=5,
- variable=radiobutton_value,
- indicatoron=0,
- command=activate
- )
- radiobutton_5_rings.grid(row=1, column=3)
- entry_ring_1 = Entry(
- root,
- justify='center'
- )
- entry_ring_1.grid(row=3, column=0)
- entry_ring_2 = Entry(root, justify='center')
- entry_ring_2.grid(row=3, column=1)
- entry_ring_3 = Entry(
- root,
- justify='center',
- state='disabled',
- disabledbackground='darkgrey'
- )
- entry_ring_3.grid(row=3, column=2)
- entry_multiplier = Entry(root, justify='center')
- entry_multiplier.grid(row=3, column=3)
- entry_tolerance = Entry(root, justify='center')
- entry_tolerance.grid(row=3, column=4)
- calculate_button = Button(
- root,
- text='Show',
- command=lambda: calculate(radiobutton_value.get()),
- state='active'
- )
- calculate_button.grid(row=4, column=2)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment