First project alarm clock

2024/11/15 0:33:59
from tkinter import *
from tkinter import ttk
from time import strftime 
import winsoundclock = Tk()clock.title("WhatAClock")clock.geometry("300x400")notebook = ttk.Notebook()tab1_timedate = Frame(notebook)tab2_alarm = Frame(notebook)tab3_timer = Frame(notebook)notebook.add(tab1_timedate, text="Time and Date")notebook.add(tab2_alarm, text="Alarm")notebook.add(tab3_timer, text="Timer")notebook.pack(expand=TRUE, fill="both")def realtime():time_str = strftime("%H:%M:%S")l1_time_timedate.config(text= time_str)l1_time_alarm.config(text= time_str)clock.after(1000, realtime)def alarm(alarm_set):while True:time_str_alarm = strftime("%H:%M:%S")if time_str_alarm == alarm_set :winsound.playsound("sound.wav",winsound.SND_ASYNC)breakdef set_alarm():alarm_set  = f"{user_h.get()}:{user_m.get()}:{user_s.get()}"alarm(alarm_set)l1_time_timedate =  Label(tab1_timedate)l1_time_alarm = Label(tab2_alarm)l1_time_timedate.place(x=20, y=30)l1_time_alarm.place(x=20, y=30)user_h = StringVar()user_m = StringVar()user_s = StringVar()entry_h = Entry(tab2_alarm, textvariable= user_h)entry_m = Entry(tab2_alarm, textvariable= user_m)entry_s = Entry(tab2_alarm, textvariable= user_s)entry_h.place(x=100, y=30)entry_m.place(x=130, y=30)entry_s.place(x=160, y=30)button_alarm = Button(tab2_alarm, command= set_alarm, text= "SET ALARM")button_alarm.place(x=100, y=70)realtime()clock.mainloop()

"Total noob again, can t figure out why the button doesn t do what it s supposed to, any clue?

Answer

There are few issues:

  • the hour, minute and second of alarm_set are not zero padded. So if hour is 1 and minute is 2 and second is 3, then alarm_set will be "1:2:3". However time_str_alarm is something like "01:02:03", therefore the checking will not be what you want.

  • don't use while loop inside alarm() as it will block tkinter mainloop() from processing pending events. Use after() like in realtime()

  • winsound.playsound(...) should be winsound.PlaySound(...) instead

Below is the modified code:

def alarm(alarm_set):time_str_alarm = strftime("%H:%M:%S")if time_str_alarm == alarm_set:# time reached, play soundwinsound.PlaySound("sound.wav",winsound.SND_ASYNC)else:# time not reached, keep checkingclock.after(1000, alarm, alarm_set)def set_alarm():# zero padded hour, minute and secondalarm_set = f"{user_h.get():02}:{user_m.get():02}:{user_s.get():02}"alarm(alarm_set)
https://en.xdnf.cn/q/119740.html

Related Q&A

Invalid Syntax using @app.route

Im getting a Invalid Syntax in line 22 @app.route(/start) and really dont know why... Im developing it under a Cloud9 server https://c9.io , maybe that has something to do with it... I tried it in two …

How do I count unique words using counter library in python?

im new to python and trying various librariesfrom collections import Counter print(Counter(like baby baby baby ohhh baby baby like nooo))When i print this the output I receive is:Counter({b: 10, : 8, …

Need some debugging in my program: filling up SQL tables with data retrieved from a Python program

I am filling up SQL tables with data that I have retrieved from a Python program. I am using Visual Studio Code for the Python program and MySQL Workbench 8.0 for SQL. There are some errors in it that …

How do I create a magic square matrix using python

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the egg…

Ensuring same dimensions in Python

The dimensions of P is (2,3,3). But the dimensions of M is (3,3). How can I ensure that both P and M have the same dimensions i.e. (2,3,3). import numpy as np P=np.array([[[128.22918457, 168.52413295,…

how to stop tkinter timer function when i press button one more times?

id tried to use root.after_cancel(AFTER), but i dont know how.root.after_cancel(AFTER) AFTER = None def countdown(count,time,name):global AFTERtime[text] =name,":",datetime.fromtimestamp(cou…

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…

How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts…

I dont understand why my script is not iterating through all string.split elements?

The objective of this python exercise is to build a function that turns text into pig latin, a simple text transformation that modifies each word by moving the first character to the end and appending …