简体   繁体   English

定时器在后台运行和调用文件

[英]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.尝试在 tkinter 中设置一个小型日程管理器,我有一个带有计时器的控制台,可以正常工作,但是我似乎无法使用我存储在 label 中的时间。

What i am trying to do is say when timer() is a certain time than do this.我想要做的是说当 timer() 是某个时间而不是这样做。

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.我找到了一个 cget function 但是我认为它只有在作为文本存储在 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.您可以安装 function 作为处理程序,然后设置一个警报,在设定的时间过去后调用该处理程序 function。 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.您注册为信号处理程序的 function 将自动传递 2 arguments:信号编号(一个int )和被信号中断的帧的堆栈帧 object。 You don't need to do anything with them, but the function must be declared to accept 2 arguments.您不需要对它们做任何事情,但必须声明 function 接受 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.signal(signal.SIGALRM, foo) == foo
True

In this case, it returns a reference to the handler function.在这种情况下,它返回对处理程序 function 的引用。 In general:一般来说:

SIG_IGN -- if the signal is being ignored SIG_IGN -- 如果信号被忽略
SIG_DFL -- if the default action for the signal is in effect SIG_DFL -- 如果信号的默认操作生效
None -- if an unknown handler is in effect无——如果一个未知的处理程序有效
anything else -- the callable Python object used as a handler其他任何东西——用作处理程序的可调用 Python object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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