0% found this document useful (0 votes)
12 views2 pages

Metric Helper

This document defines a Python program that creates a graphical user interface (GUI) for converting between different metric prefixes. It defines functions to convert values between prefixes, lays out the GUI elements like input/output fields and dropdown menus, and includes a button to trigger the conversion function. The GUI allows users to enter a quantity, select the start and end prefixes, and see the converted value.

Uploaded by

Manuel Developer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Metric Helper

This document defines a Python program that creates a graphical user interface (GUI) for converting between different metric prefixes. It defines functions to convert values between prefixes, lays out the GUI elements like input/output fields and dropdown menus, and includes a button to trigger the conversion function. The GUI allows users to enter a quantity, select the start and end prefixes, and see the converted value.

Uploaded by

Manuel Developer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #Metric Helper

2 import tkinter
3 from tkinter import ttk, END
4
5 #Define window
6 root = tkinter.Tk()
7 root.title('Metric Helper')
8 root.iconbitmap('ruler.ico')
9 root.resizable(0,0)
10
11 #Define fonts and colors
12 field_font = ('Cambria', 10)
13 bg_color = "#c75c5c"
14 button_color = "#f5cf87"
15 root.config(bg=bg_color)
16
17 #Define functions
18 def convert():
19 """Convert from one metric prefix to another"""
20 metric_values = {
21 'femto':10**-15,
22 'pico':10**-12,
23 'nano':10**-9,
24 'micro':10**-6,
25 'milli':10**-3,
26 'centi':10**-2,
27 'deci':10**-1,
28 'base value':10**0,
29 'deca':10**1,
30 'hecto':10**2,
31 'kilo':10**3,
32 'mega':10**6,
33 'giga':10**9,
34 'tera':10**12,
35 'peta':10**15
36 }
37
38 #Clear the output field
39 output_field.delete(0, END)
40
41 #Get all user information
42 start_value = float(input_field.get())
43 start_prefix = input_combobox.get()
44 end_prefix = output_combobox.get()
45
46 #Covert to the base unit first
47 base_value = start_value*metric_values[start_prefix]
48 #Covert to new metric value
49 end_value = base_value/metric_values[end_prefix]
50
51 #Update ouput field with answer
52 output_field.insert(0, str(end_value))
53
54
55 #Define layout
56 #Create the input and output entry fields
57 input_field = tkinter.Entry(root, width=20, font=field_font, borderwidth=3)
58 output_field = tkinter.Entry(root, width=20, font=field_font, borderwidth=3)
59 equal_label = tkinter.Label(root, text="=", font=field_font, bg=bg_color)
60
61 input_field.grid(row=0, column=0, padx=10, pady=10)
62 equal_label.grid(row=0, column=1, padx=10, pady=10)
63 output_field.grid(row=0, column=2, padx=10, pady=10)
64
65 input_field.insert(0, 'Enter your quantity')
66
67 #Create combobox for metric values
68 metric_list = ['femto', 'pico', 'nano', 'micro', 'milli', 'centi', 'deci', 'base
value', 'deca', 'hecto', 'kilo', 'mega', 'giga', 'tera', 'peta']
69 input_combobox = ttk.Combobox(root, value=metric_list, font=field_font, justify='center')
70 output_combobox = ttk.Combobox(root, value=metric_list, font=field_font,
justify='center')
71 to_label = tkinter.Label(root, text="to", font=field_font, bg=bg_color)
72
73 input_combobox.grid(row=1, column=0, padx=10, pady=10)
74 to_label.grid(row=1, column=1, padx=10, pady=10)
75 output_combobox.grid(row=1, column=2, padx=10, pady=10)
76
77 input_combobox.set('base value')
78 output_combobox.set('base value')
79
80 #Create a conversion button
81 convert_button = tkinter.Button(root, text='Convert', font=field_font, bg=button_color,
command=convert)
82 convert_button.grid(row=2, column=0, columnspan=3, padx=10, pady=10, ipadx=50)
83
84 #Run the root window's main loop
85 root.mainloop()

You might also like