-4

I have my GUI App developed in Python 2.7 . Application will take start time, end time and time interval from user.I have code which will take this input and create a array of time as per the interval. I am trying to implement here a function which will get called at the time which is stored in array and can also be stopped if user press stop button.

time_slots = [dt.strftime('%H:%M:%S') for dt in
    datetime_range(start_time, 
    end_time,timedelta(minutes=interval))]

time_slots_array = np.array(time_slots)

def function1():
    print("In function 1")

def start():
    global start_time,end_time,time_slots

    now_time = datetime.now().time()
    t.sleep(1)
    keys = sorted(time_slots_arrary
    if now_time >= start_time.time() and now_time <= end_time.time():
       if now.strftime("%H:%M:%S") in keys:
          function1()
       job1 = root.after(1000, start())

    else:
     root.after_cancel(job1)

start_button = tk.Button(prod_frame, text='Start Program ', width=25, background='green', foreground='black',command = start)

stop_button = tk.Button(prod_frame, text='Stop Program ', width=25, background='orange', foreground='black',command = stop)`
S K
  • 7
  • 5
  • Look into the `Tk.after` and `Tk.after_cancel` methods. – Novel Dec 11 '17 at 18:08
  • 3
    there are lots of questions and answers on this site related to creating timers in tkinter. Did you do any research before asking? – Bryan Oakley Dec 11 '17 at 18:14
  • @BryanOakley Yes I have researched and tried the solution which were listed but application was crashing. – S K Dec 11 '17 at 18:31
  • If it was crashing, the error messages were telling you something useful. Perhaps if you show what you tried along with the error message, we might be able to help. – Bryan Oakley Dec 11 '17 at 18:35
  • @BryanOakley I have included the tk.after and Tk.after_cancel also .App works at first.When I press start button which is supposed to call start function then it freezes and crashes – S K Dec 11 '17 at 19:59
  • always put full error message (Traceback) in question (as text, not screenshot). There are other useful informations. – furas Dec 11 '17 at 20:13
  • `after()` expects `callback` - it meas function name without `()`. You need `after(1000, start)` instead of `after(1000, start() )` – furas Dec 11 '17 at 20:22

1 Answers1

1

You're calling after incorrect. It requires a callable -- a reference to a function.

Consider this line of code:

job1 = root.after(1000, start())

The above code is functionality identical to this:

result = start()
job1 = root.after(1000, result)

And since start() returns None, it's the same as this:

start()
job1 = root.after(1000, None)

The correct way to call after is by giving it a reference to the function:

job1 = root.after(1000, start)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685