Python Tkinker events [duplicate] - python

This question already has answers here:
How can I schedule updates (f/e, to update a clock) in tkinter?
(8 answers)
Closed 8 years ago.
I would want to use time to enable event's callback function in Tkinter.
How do I do something like this using Tkinter
ftime = time()
while 1:
if ftime - time() > 2000:
dosomething
ftime = time()
Note that all I wanted is being able to use time passed to call callback function
Janus

You can use the Tkinter method after to schedule a command to run after a given number of milliseconds. That's considerably better than implementing a tight loop. Remember: the event loop is already an infinite loop, nesting a long running loop inside it causes performance problems.
If you want something to be called more than once after a given period of time you can have a job call itself at regular intervals. There is an example in this answer to the question How to create a timer using tkinter?

Related

A general programming question: Is there a way to trigger a function every x seconds without remembering the last time it was triggered? [duplicate]

This question already has answers here:
How do I get a Cron like scheduler in Python?
(9 answers)
Closed last month.
Sorry if this is a basic question, but I want to have a function trigger every fixed amount of time, say 2 seconds. In general I would do something like:
lifetime = getCurrentTime
if (lifetime - lastTime > triggerTime)
doTheThing()
lastTime = lifetime
end
This normally works fine, but I'm working on a program where each object stores its own lifetime, and there could be hundreds of objects at once. I'm wondering if there's a way that each object wouldn't need to also remember its own lastTime -- I was thinking something with rounding or modulo, but even then you'd still need to remember how many times the function had triggered before.
I'm wondering if anyone knows of a good general way to have a trigger function every x seconds without having to remember the last time it triggered or how many times it's triggered?
Have you tried using sched yet? Here's a task done every 5 seconds to get you started:
import sched
import time
def doTheThing(runnable_task, every):
runnable_task.enter(every, 1, doTheThing, (runnable_task, every))
print("The thing...")
task = sched.scheduler(time.time, time.sleep)
every = 5
task.enter(every, 1, doTheThing, (task, every))
task.run()

Is it possible to cancel an exec() in python after 5 seconds [duplicate]

This question already has answers here:
Timeout on a function call
(23 answers)
Closed 4 months ago.
I am working on a python script that runs small user generated scripts. There is a problem though when a user submits a script that infinitely loops. I was wondering if it would be possible to stop an exec() after 5 seconds of running.
x = exec(user_code)
delay(5)
x.cancel()
something like this ^
So far I tried using threading but the results were mixed. I threaded a function called main which would run their code through exec then I would delete the thread after 5 seconds but it was buggy.
You could use multiprocessing:
import multiprocessing
import time
def exec(n):
for i in range(10000 * n):
print("Whatever") #Your code
if __name__ == '__main__':
process = multiprocessing.Process(target=exec, name="Exec", args=(10,))
process.start()
time.sleep(1) #How much to wait
process.terminate()
process.join()

Python while loop be executed in time interval [duplicate]

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 2 years ago.
I wanted to use while loop to keep executing my script over and over but there is a problem as my code calls an API that doesn't allow so many calls in small time so I wanted to make the while loop be executed at time interval so I tried this code
from threading import Timer
def myfunc():
some code
while True:
t = Timer(1.0, myfunc)
t.start()
but it doesn't work, so is there any other way to do it correctly?
Use the time module:
import time
def myfunc():
some code
while True:
myfunc()
# unit is in second. Example below wait for 1 second before continuing
time.sleep(1)

Using sleep() to create a function that consistently loops every second [duplicate]

This question already has answers here:
How to repeatedly execute a function every x seconds?
(22 answers)
Closed 4 years ago.
I have a program that needs to execute every second. However I am concerned that the code would add a slight delay in turn causing it delay slightly longer then intended. Sample code:
while True:
print(time)
sleep(1)
In my case I will be adding more complicated function calls in this loop and am concerned that they will mess with my timer. Should I even be worried, and or is there another way for me to ensure this function loops every second?
You can use this:
import threading
def scheduleFunc():
threading.Timer(1.0, scheduleFunc).start()
print(time)
Or use this:
import sched, time
scheduled = sched.scheduler(time.time, time.sleep)
def scheduleFunc(sc):
print(time)
scheduled.enter(60, 1, scheduleFunc, (sc,))
scheduled.enter(60, 1, scheduleFunc, (scheduled,))
scheduled.run()

How to repeat a function every N minutes? [duplicate]

This question already has answers here:
How to repeatedly execute a function every x seconds?
(22 answers)
Closed 7 years ago.
In my python script I want to repeat a function every N minutes, and, of course, the main thread has to keep working as well. In the main thread I have this:
# something
# ......
while True:
# something else
sleep(1)
So how can I create a function (I guess, in another thread) which executes every N minutes? Should I use a timer, or Even, or just a Thread? I'm a bit confused.
use a thread
import threading
def hello_world():
threading.Timer(60.0, hello_world).start() # called every minute
print("Hello, World!")
hello_world()

Categories