-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime conversion.py
108 lines (96 loc) · 3.98 KB
/
Time conversion.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from tkinter import Tk, StringVar, END
from tkinter.ttk import *
import pyautogui
screenWidth, screenHeight = pyautogui.size()
main = Tk()
main.title("Unit conversion")
main.minsize(320, 200)
main.geometry("{}x{}+{}+{}".format(320, 200, (screenWidth - 320) // 2, (screenHeight - 200) // 2))
Label(main, text="Select the original unit", foreground="blue").pack(side="top")
frame = Frame(main)
frame.pack(side="top")
def only_numbers(chars):
return all(char.isdigit() or char == '.' for char in chars)
validation = main.register(only_numbers)
userInput = Entry(frame, width=10, validate="key", validatecommand=(validation, "%P"))
userInput.pack(side="left")
originalUnit = StringVar()
dropdown0 = OptionMenu(frame, originalUnit, "Minutes", "Yocto-seconds", "Zepto-seconds", "Atto-seconds", "Femto-seconds",
"Picoseconds", "Nanoseconds", "Microseconds", "Milliseconds", "Seconds", "Minutes", "Hours",
"Days", "Weeks", "Months", "Years", "Decades", "Centuries", "Millennia")
dropdown0.pack(side="left")
Label(main, text="Select the modified unit", foreground="blue").pack(side="top")
modifiedUnit = StringVar()
dropdown1 = OptionMenu(main, modifiedUnit, "Seconds", "Yocto-seconds", "Zepto-seconds", "Atto-seconds", "Femto-seconds",
"Picoseconds", "Nanoseconds", "Microseconds", "Milliseconds", "Seconds", "Minutes", "Hours",
"Days", "Weeks", "Months", "Years", "Decades", "Centuries", "Millennia")
dropdown1.pack(side="top")
# noinspection PyGlobalUndefined
def convert():
global operator, x, y
if all(char == '.' for char in userInput.get()):
userInput.delete(0, END)
userInput.insert(0, "0")
times = ["Yocto-seconds", "Zepto-seconds", "Atto-seconds", "Femto-seconds", "Picoseconds", "Nanoseconds",
"Microseconds", "Milliseconds", "Seconds", "Minutes", "Hours", "Days", "Weeks", "Months", "Years",
"Decades", "Centuries", "Millennia"]
firstValue = times.index(originalUnit.get())
secondValue = times.index(modifiedUnit.get())
if firstValue == secondValue:
result = userInput.get()
else:
if firstValue > secondValue:
x = firstValue
y = secondValue
operator = "*"
elif secondValue > firstValue:
x = secondValue
y = firstValue
operator = "/"
result = float(userInput.get())
while True:
x -= 1
if x <= 7:
if operator == "*":
result *= 1000
if operator == "/":
result /= 1000
elif x == 8 or x == 9:
if operator == "*":
result *= 60
if operator == "/":
result /= 60
elif x == 10:
if operator == "*":
result *= 24
if operator == "/":
result /= 24
elif x == 11:
if operator == "*":
result *= 7
if operator == "/":
result /= 7
elif x == 12:
if operator == "*":
result *= 4
if operator == "/":
result /= 4
elif x == 13:
if operator == "*":
result *= 12
if operator == "/":
result /= 12
elif x >= 14:
if operator == "*":
result *= 10
if operator == "/":
result /= 10
if x <= y:
break
userInput.delete(0, END)
userInput.insert(0, "{}".format(result))
storedValue = originalUnit.get()
originalUnit.set(modifiedUnit.get())
modifiedUnit.set(storedValue)
Button(main, text="Convert to modified unit", command=convert).pack(side="top")
main.mainloop()