15.PyStopWatch.py

 # 1. import the Tkinter and ttkthemes modules

# 2. We need a window with following elements:
# -timer label
# - button for starting/stopping
# -button for resetting the timer to 0
# - function 'count' to update the timer display every second while running
# -the time should displayed in the 'h:m:s' format
import tkinter as tk
from ttkthemes import ThemedStyle


class Stopwatch(tk.Tk):
def __init__(self):
super().__init__()
self.title("Py StopWatch")
self.geometry("500x190")
self.time = 0
self.running = False

self.style = ThemedStyle(self)
self.style.set_theme("adapta")

self.label = tk.Label(self, text="00:00:00", pady=5, padx=5, font=("ds-digital", 50, 'bold'), bg="yellow", fg="green", width=20, height=1)
self.label.pack()

self.alarm_label = tk.Label(self, text="TIME'S UP!", font=("Arial", 12), fg="grey", )
self.alarm_label.pack(pady=10)

self.start_button = tk.Button(self, text="START", width=8, height=1, font=("digital-7", 14, 'bold'), command=self.start)
self.start_button.pack(side=tk.LEFT)

self.stop_button = tk.Button(self, text="STOP", pady=5, padx=5, width=5, height=1, font=("Arial", 14, 'bold'), command=self.stop)
self.stop_button.place(x=210, y=135)


self.reset_button = tk.Button(self, text="RESET", width=8, height=1, font=("Arial", 14, 'bold'), command=self.reset)
self.reset_button.pack(side=tk.RIGHT)


def start(self):
self.running = True
self.count()
self.alarm_label.config(text="TIME RUNNING")

def stop(self):
self.running = False
self.alarm_label.config(text="TIME'S UP!")


def reset(self):
self.running = False
self.time = 0
self.label.config(text="00:00:00")
self.alarm_label.config(text="LETS START")

def count(self):
if self.running:
self.time += 1
minutes, seconds = divmod(self.time, 60)
hours, minutes = divmod(minutes, 60)
self.label.config(text="{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds))
self.after(100, self.count)


if __name__ == '__main__':
stopwatch = Stopwatch()
stopwatch.mainloop()





# # #To Create a graphical window it is root window so obj name root is created here from tkinter class
# root = tk.Tk() # Note the correct capitalization for 'Tk'
#
# # To set the title
# root.title("Python Stop Watch")
#
# ur_time = int(input("Enter your time in minutes: "))
# # time_in_sec = ur_time / 60
#
# for i in range(ur_time,0,-1):
# seconds = i % 60
# minutes = int(i / 60)
# hours = int(i / 3600)
# print(f"{hours}:{minutes:02}:{seconds:02}")
# time.sleep(1)
#
# print("Time is Up!")

Comments

Popular posts from this blog

3.FaulyCalculator.py

11.Snake Water Gun Game.py