Python Forum

Full Version: messagebox is not being executed please help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello
I am trying to create my first Alarm Clock but I've stuck. I've simply created Tkinter window with a clock and some entry boxes and button to set the alarm time. Now:
1. The clock is working and its type is a string
2. Entry boxes are taking entered values and (after the "Set Time" button clicked) they are being shown in the grey label also as a string type.
3. I've made another def alarm(): to define that if the clock string is in the alarm time string then I want to message box to appear but for some reason, I have no errors and message box doesn't appear

can you please review my code and point me why showinfo() message box is no being executed.

import tkinter as tk
import time
import datetime
from tkinter.messagebox import *


def set_time():
    hour = entry_hour.get()
    minutes = entry_minutes.get()
    seconds = entry_seconds.get()
    time_set_label["text"] = hour.zfill(
        2) + ":" + minutes.zfill(2) + ":" + seconds.zfill(2)


window = tk.Tk()
window.title('Alarm Clock')
window.geometry('500x140')
window.resizable(width=False, height=False)

clock_label = tk.Label(master=window, background="gray",
                       font=("Arial", 40), width=10, height=1)
clock_label.place(x=100, y=0)

entry_hour = tk.Entry(width=4)
entry_hour.insert(0, "Hr")
entry_hour.place(x=10, y=70)
entry_minutes = tk.Entry(width=4)
entry_minutes.insert(0, "Min")
entry_minutes.place(x=50, y=70)
entry_seconds = tk.Entry(width=4)
entry_seconds.insert(0, "Sec")
entry_seconds.place(x=90, y=70)

set_time_button = tk.Button(master=window, width=7, height=1,
                            text="Set Time", padx="-10", command=set_time)
set_time_button.place(x=35, y=90)

time_set_label = tk.Label(master=window, background="gray", font=(
    "Arial", 40), width=9, height=1)
time_set_label.place(x=120, y=70)


def alarm():
    if time_set_label["text"] in clock_label["text"]:
        showinfo("Alarm", "Alarm!")


def tick():
    clock_label["text"] = time.strftime("%H:%M:%S")
    clock_label.after(200, tick)


tick()


window.mainloop()
You need to test the alarm periodically. I think the alarm() code belongs in the tick() code. And change the test for if it is time for the alarm to go of or you will be surprised
You never call alarm function