Import Tkinter As TK Time

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

import tkinter as tk

from tkinter import messagebox

class TimeMaster:

def __init__(self, root):

# Initialize the TimeMaster class with the root Tkinter window

self.root = root

self.root.title("Time Master")

self.root.geometry("250x350")

self.root.configure(bg='black')

# Initialize minutes and seconds

self.minutes = 0

self.seconds = 0

# Create and configure the timer display label

self.timer_label = tk.Label(root, text="00:00",bg='black',fg='white', font=("Arial", 24))

self.timer_label.pack()

# Create labels and entry widgets for setting minutes and seconds

self.minutes_label = tk.Label(root, text="Minutes:",bg='black',fg='white')

self.minutes_label.pack()

self.minutes_entry = tk.Entry(root)

self.minutes_entry.pack()

self.seconds_label = tk.Label(root, text="Seconds:",bg='black',fg='white')

self.seconds_label.pack()
self.seconds_entry = tk.Entry(root)

self.seconds_entry.pack()

# Create buttons for setting timer, starting/pausing, and resetting

self.set_timer_button = tk.Button(root, text="Set Timer",bg='gray',command=self.set_timer)

self.set_timer_button.pack(padx=10,pady=10)

self.start_button = tk.Button(root, text="Start", bg='green', command=self.start_pause)

self.start_button.pack(padx=5,pady=5)

self.reset_button = tk.Button(root, text="Reset",bg='pink' ,command=self.reset)

self.reset_button.pack(padx=5,pady=5)

# Initialize timer ID and pause state

self.timer_id = None

self.is_paused = False

def set_timer(self):

# Set the timer based on user input from entry widgets

self.minutes = int(self.minutes_entry.get())

self.seconds = int(self.seconds_entry.get())

self.update_timer_display()

def start_pause(self):

# Start or pause the timer based on the current state

if self.timer_id is None or self.is_paused:

self.start_timer()

else:

self.pause_timer()
def start_timer(self):

# Start the timer and update the button text

self.start_button.config(text="Pause")

self.timer_id = self.root.after(1000, self.update_timer)

self.is_paused = False

def pause_timer(self):

# Pause the timer and update the button text

self.start_button.config(text="Resume")

self.root.after_cancel(self.timer_id)

self.timer_id = None

self.is_paused = True

def reset(self):

# Reset the timer, entry widgets, and update the button text

self.root.after_cancel(self.timer_id)

self.minutes_entry.delete(0, tk.END)

self.seconds_entry.delete(0, tk.END)

self.minutes = 0

self.seconds = 0

self.is_paused = False

self.start_button.config(text="Start")

self.update_timer_display()

def update_timer(self):

# Update the timer display and continue counting down

if self.minutes > 0 or self.seconds > 0:

if self.seconds == 0:
self.minutes -= 1

self.seconds = 59

else:

self.seconds -= 1

self.update_timer_display()

self.timer_id = self.root.after(1000, self.update_timer)

else:

# Timer reached 00:00, show a message and reset

self.show_time_over_message()

self.reset()

def update_timer_display(self):

# Update the timer label with the current time

timer_text = f"{self.minutes:02d}:{self.seconds:02d}"

self.timer_label.config(text=timer_text)

def show_time_over_message(self):

# Show a message when the timer reaches 00:00

messagebox.showinfo("Time's Up", "Time is over!")

# Create the Tkinter root window and TimeMaster instance

root = tk.Tk()

time_master = TimeMaster(root)

# Start the Tkinter event loop

root.mainloop()

You might also like