forked from crazycodersonline/Python-Beginner-GUI-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_converter_gui.py
65 lines (45 loc) · 1.49 KB
/
unit_converter_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from tkinter import *
root = Tk()
root.title("Unit Converter")
Units = ['mm' , 'cm' , 'm']
menu = StringVar()
menu.set("mm")
dropmenu1 = OptionMenu(root , menu , *Units)
dropmenu1.grid(row=1 , column=1)
menu2 = StringVar()
menu2.set("mm")
dropmenu2 = OptionMenu(root , menu2, *Units)
dropmenu2.grid(row=1 , column=2)
input = Entry(root,font=(20))
input.grid(row=2, column=1)
output = Label(root,width = 20,borderwidth=3 , relief="solid")
output.grid(row=2 , column=2)
def Clear():
input.delete(0,"end")
output.config(text = "")
def Convert():
unit1 = menu.get()
unit2 = menu2.get()
try:
num = int(input.get())
converted_num = num
if unit1 == 'mm' and unit2 == 'cm':
converted_num = num/10
elif unit1 == 'mm' and unit2 == 'm':
converted_num = num/1000
elif unit1 == 'cm' and unit2 == 'mm':
converted_num = num*10
elif unit1 == 'cm' and unit2 == 'm':
converted_num = num/100
elif unit1 == 'm' and unit2 == 'mm':
converted_num = num*1000
elif unit1 == 'm' and unit2 == 'cm':
converted_num = num*100
output.config(text=str(converted_num))
except:
output.config(text="Invalid Input")
b_convert = Button(root , text = "Convert",command = Convert)
b_convert.grid(row=3 , column=1)
b_clear = Button(root, text="Clear" , command=Clear)
b_clear.grid(row=3,column=2)
root.mainloop()