简体   繁体   中英

Timer to run in background and call files

Trying to set up a small schedule manager in tkinter, I have a console with a timer that works fine however i cant seem to be able to use the time i have stored in a label.

What i am trying to do is say when timer() is a certain time than do this.

im new to this and trying my hardest to learn. I have found a cget function however i think it only returns when stored as text in a label. If someone could point me in the right direction?

from tkinter import *
import sys
import time
from time import strftime


#function to close window when ESC is pressed
def close(event):
    sys.exit()

#function to get timer to calculate
def timer():
    timer_tick = strftime("%H:%M:%S")
    time_label.configure(text=timer_tick)
    time_label.after(1000, timer)

########## MAIN CONSOLE WINDOW
window = Tk()
ws=window.winfo_screenwidth()
hs=window.winfo_screenheight()
#with bar w=780
#with bar h=100
w=565
h=66
x=(ws/1)-(w/1)
y=(hs/1)-(h/1)

#window.state('zoomed')
window.configure(bg='#3a3a3a')
window.wm_attributes("-topmost", 1)
window.geometry('+%d+%d'%(x,y))
window.overrideredirect(1)

########## FRAMES
top_frame = Frame(window)
top_frame.pack(side=TOP)

########## LABELS
header_label = Label(top_frame, text="Automated Job Runner", width=30, height=1, borderwidth=3, anchor="w", background='#3a3a3a', fg='#ffffff', font=("calibri", 11))
header_label.pack(side=LEFT)

time_label = Label(top_frame, width=10, height=1, borderwidth=3, background='#3a3a3a', fg='#ffffff', font=("calibri", 11))
time_label.pack(side=LEFT) 

status_label = Label(top_frame, text="Status: ", width=10, height=1, borderwidth=3, anchor="e", background='#3a3a3a', fg='#ffffff', font=("calibri", 11))
status_label.pack(side=LEFT)

status_var = Label(top_frame, text="TBC", width=10, height=1, borderwidth=3, anchor="w", background='#3a3a3a', fg='#ffffff', font=("calibri", 11))
status_var.pack(side=LEFT)

panel_button = Button(top_frame, text ="Console", background='#3a3a3a', fg='#ffffff', font=("calibri", 10))
panel_button.pack(side=LEFT)

def schedules():
    while 1+1==2:
        time.sleep(1)
        if timer() == '20:00':
            'do this'


if __name__ == "__main__":
    timer()

#on press ESC window closes
window.bind('<Escape>', close)
schedules()
window.mainloop()

One way to do this is using signals. You can install a function as a handler, then set an alarm that will call that handler function after a set time has elapsed. You can use it like this:

>>> import signal
>>> def foo(sig, frame):
...   print(f"got signal {sig} inside stack frame {frame}")
...
>>> signal.signal(signal.SIGALRM, foo)
<Handlers.SIG_DFL: 0>
>>> signal.alarm(5)
>>> # 5 seconds later...
>>>
0
got signal 14 inside stack frame None
>>>

The function that you register as the signal handler will automatically be passed 2 arguments: the signal number (an int ) and the stack frame object of the frame that was interrupted by the signal. You don't need to do anything with them, but the function must be declared to accept 2 arguments.

This is truly asynchronous, so at no point does the control flow stop and wait for the signal to happen (which means you might need to use an event loop to keep the program running).

In case you were wondering what signal.signal is returning:

>>> signal.signal(signal.SIGALRM, foo) == foo
True

In this case, it returns a reference to the handler function. In general:

SIG_IGN -- if the signal is being ignored
SIG_DFL -- if the default action for the signal is in effect
None -- if an unknown handler is in effect
anything else -- the callable Python object used as a handler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM