Home > Net >  Alarmclock first project won t work (exeption in tkinter callback - ValueError: '=' alignm
Alarmclock first project won t work (exeption in tkinter callback - ValueError: '=' alignm

Time:10-14

from tkinter import *    
from time import strftime
from tkinter import ttk
import winsound

clock = 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):
    time_str_alarm = strftime("%H:%M:%S")
    if time_str_alarm == alarm_set:
        winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
    else:
        clock.after(1000,alarm)            

def set_alarm():    
    alarm_set  =  f"{user_h.get():02}:{user_m.get():02}:{user_s.get():02}"
    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()

Still can t make it work, i m totally out of ideas. I crosschecked with another alarmclock program, it s a simpler one, still, the alarm part of mine would seem it should work to me...

Edit: maybe i should clarify that the ":02" part was suggested to solve the 0 padding, but the program i used to crosscheck did not have this.

Edit: copy pasting the error after changing the "f"string to: f"{int(user_h.get()):02}:{int(user_m.get()):02}:{int(user_s.get()):02}"

Error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Spyder\pkgs\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Program Files\Spyder\pkgs\tkinter\__init__.py", line 814, in callit
    func(*args)
TypeError: alarm() missing 1 required positional argument: 'alarm_set'

CodePudding user response:

The error suggest that you did not specify arguments that are needed to run your function. Even more specific, it tells you that this happens in your after routine if you know that callit is part of this method.

compare source code:

def after(self, ms, func=None, *args):
    """Call function once after given time.
    MS specifies the time in milliseconds. FUNC gives the
    function which shall be called. Additional parameters
    are given as parameters to the function call.  Return
    identifier to cancel scheduling with after_cancel."""
    if func is None:
        # I'd rather use time.sleep(ms*0.001)
        self.tk.call('after', ms)
        return None
    else:
        def callit():
            try:
                func(*args)
            finally:
                try:
                    self.deletecommand(name)
                except TclError:
                    pass
        try:
            callit.__name__ = func.__name__
        except AttributeError:
            # Required for callable classes (bpo-44404)
            callit.__name__ = type(func).__name__
        name = self._register(callit)
        return self.tk.call('after', ms, name)

In the __doc__ string of this method is also the solution to this error. You can parse arguments to the callit. So the suggested change would be:

#clock.after(1000,alarm)
clock.after(1000, alarm, alarm_set) 

Don't forget to accept this answer if it solves your question.

  • Related