I'm trying to write a shutdown timer in Python as a newbie programmer. The purpose of the program is to accept a time in a GUI entry box and when the user clicks the shutdown/restart button to start a timer, showing the remaining time in the same window.
Currently, I've got the program to basically function but I don't know how to make the timer update properly (except in a crude print() timer). I see that there are some answers on other programming languages but I'm not yet fluent in those. Also, as I've said, I'm a new programmer so if you see anything else that I could do better I would welcome constructive criticism.
# This program accepts a time value for shutdown/restart
# counts down, shutting down the system at the end of the timer.
import tkinter
import time
import os
class ShutdownTimerGUI:
def __init__(self):
# Create the main window.
self.main_window = tkinter.Tk()
# Create three frames to group widgets.
self.top_frame = tkinter.Frame()
self.mid_frame = tkinter.Frame()
self.bottom_frame = tkinter.Frame()
# Create the widgets for the top frame.
self.prompt_label = tkinter.Label(self.top_frame, \
text='Enter the number of minutes until shutdown:')
self.time_entry = tkinter.Entry(self.top_frame, \
width=3)
# Pack the top frame's widgets.
self.prompt_label.pack(side='left')
self.time_entry.pack(side='left')
# Create the widgets for the middle frame.
self.descr_label = tkinter.Label(self.mid_frame, \
text='Time until shutdown:')
# Associate a StringVar object with the output label. Use the object's
# set method to store a string of blank characters.
self.value = tkinter.StringVar()
# Create a label and associate it with the StringVar object. Any
# value stored in the StringVar object will automatically be displayed
# in the label.
self.time_label = tkinter.Label(self.mid_frame, \
textvariable=self.value)
# Pack the middle frame's widgets.
self.descr_label.pack(side='left')
self.time_label.pack(side='left')
# Create the button widgets for the bottom frame.
self.shutdown_button = tkinter.Button(self.bottom_frame, \
text='Shutdown', \
command=self.shutdown)
self.restart_button = tkinter.Button(self.bottom_frame, \
text='Restart', \
command=self.restart)
self.shutdown_button.pack()
self.restart_button.pack()
# Pack the buttons.
self.top_frame.pack()
self.mid_frame.pack()
self.bottom_frame.pack()
#Enter the tkinter main loop.
tkinter.mainloop()
# The shutdown method is a callback function for the Shutdown Button.
def shutdown(self):
# Get the value entered by the user into the time_entry widget.
minutes = float(self.time_entry.get())
seconds = minutes * 60
count = 0
self.time_left()
os.system('shutdown /s')
# The restart method is a callback function for the Restart Button.
def restart(self):
# Get the value entered by the user into the time_entry widget.
minutes = float(self.time_entry.get())
seconds = minutes * 60
count = 0
self.time_left()
os.system('shutdown /r')
def time_left(self):
minutes = float(self.time_entry.get())
seconds = minutes * 60
count = 0
while count < seconds:
time.sleep(1)
count += 1
time_left = seconds - count
print(time_left)
self.value.set(time_left)
Shutdown_Timer = ShutdownTimerGUI()