Skip to content

Commit

Permalink
countdown input working
Browse files Browse the repository at this point in the history
  • Loading branch information
estevE11 committed Sep 16, 2022
1 parent 1312d5e commit 2980d30
Showing 1 changed file with 73 additions and 13 deletions.
86 changes: 73 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tkinter as tk
import os
from datetime import datetime
import math

def get_sec_to_time(h, m, hh, mm):
now = datetime.now()
Expand All @@ -14,18 +15,27 @@ def get_sec_to_time(h, m, hh, mm):
dm = m-mm
sec = abs(dh*3600+dm*60)

#print("Current Time =", hh, mm)
#print("Selected Time =", h, m)
#print("Result =", sec)
return sec

seconds = now.timestamp()
test = now.fromtimestamp(seconds)
test2 = now.fromtimestamp(seconds + sec)
#print("Current time =", str(test).split(" ")[1])
#print("Calculated time", str(test2).split(" ")[1])
def calc_sec_to_time(sec):
h=sec//3600
m=(sec%3600)//60
return h, m

return sec
def sum_time_to_current_time(h, m):
now = datetime.now()
ch = int(now.strftime("%H"))
cm = int(now.strftime("%M"))

res_h = ch + h

mm = cm + m
res_m = mm
if mm > 59:
res_h += 1
res_m = mm - 60

return res_h, res_m

def set_time():
now = datetime.now()
Expand All @@ -41,19 +51,41 @@ def cancel_time():
os.system("shutdown -a")

def cicle_hours():
check_0_val.set(True)
check_1_val.set(False)
selected_hour = int(spin_hour.get())
if selected_hour < 0:
hour.set(23)
elif selected_hour > 23:
hour.set(0)

def cicle_min():
check_0_val.set(True)
check_1_val.set(False)
selected_min = int(spin_min.get())
if selected_min < 0:
min.set(59)
elif selected_min > 59:
min.set(0)

def cicle_hours_disc():
check_0_val.set(False)
check_1_val.set(True)
selected_hour_disc = int(spin_hour_disc.get())
if selected_hour_disc < 0:
hour_disc.set(23)
elif selected_hour_disc > 23:
hour_disc.set(0)

def cicle_min_disc():
check_0_val.set(False)
check_1_val.set(True)
selected_min_disc = int(spin_min_disc.get())
if selected_min_disc < 0:
min_disc.set(59)
elif selected_min_disc > 59:
min_disc.set(0)

def check_0_trigger():
if not check_0_val.get():
check_0_val.set(True)
Expand All @@ -65,7 +97,34 @@ def check_1_trigger():
check_1_val.set(True)
return
check_0_val.set(False)


def update_time():
if check_0_val.get():
update_countdown()
else:
update_normal_time()
root.after(1000, update_time)

def update_normal_time():
selected_hour_disc = int(spin_hour_disc.get())
selected_min_disc = int(spin_min_disc.get())
h, m = sum_time_to_current_time(selected_hour_disc, selected_min_disc)
hour.set(h)
min.set(m)

def update_countdown():
now = datetime.now()

selected_hour = int(spin_hour.get())
selected_min = int(spin_min.get())

sec = get_sec_to_time(selected_hour, selected_min, int(now.strftime("%H")), int(now.strftime("%M")))

h, m = calc_sec_to_time(sec)

hour_disc.set(h)
min_disc.set(m)


root = tk.Tk()
root.resizable(False, False)
Expand Down Expand Up @@ -97,16 +156,17 @@ def check_1_trigger():

hour_disc = tk.StringVar(root)
hour_disc.set(_now.strftime("%H"))
spin_hour_disc = tk.Spinbox(frm, increment=1, from_=-1, to=24, textvariable=hour, command=cicle_hours)
spin_hour_disc = tk.Spinbox(frm, increment=1, from_=-1, to=24, textvariable=hour_disc, command=cicle_hours_disc)
spin_hour_disc.grid(column=0, row=3)

min_disc = tk.StringVar(root)
min_disc.set(_now.strftime("%M"))
min_disc = tk.Spinbox(frm, increment=1, from_=-1, to=60, textvariable=min, command=cicle_min)
min_disc.grid(column=1, row=3)
spin_min_disc = tk.Spinbox(frm, increment=1, from_=-1, to=60, textvariable=min_disc, command=cicle_min_disc)
spin_min_disc.grid(column=1, row=3)

time_btn = tk.Button(frm, text="Schedule shutdown", command=set_time).grid(column=0, row=4)

cancel_btn = tk.Button(frm, text="Cancel shutdown", command=cancel_time).grid(column=1, row=4)

update_time()
root.mainloop()

0 comments on commit 2980d30

Please sign in to comment.