Background Information - I'm attempting to create somewhat of animation for a frame object with TKinter with the following code:
from tkinter import Frame, Tk, Label, Button
import time
def runAnim():
for width in range(0, 200):
app.after(5000, lambda width = width: test_label.config(width=width))
app = Tk()
app.geometry("500x500")
test_label = Frame(bg="#222", width=0)
test_label.pack(side="left", fill="y")
test_button = Button(text="toggle", command=lambda: runAnim() )
test_button.pack(side="right")
The problem is that it this is not producing the desired behaviour. My understanding is that this should gradually increase the width every 5 seconds, however the 0-200 range seems to complete within these 5 seconds, rather than it being an increased width of 1 every 5 seconds.
Any solutions would be appreciated!
That after(5000, …) means 5 seconds after right now, as after is being called, not 5 seconds after some future point in time that tkinter can only guess by reading your mind.
So, you're just creating 200 callbacks, and scheduling them all to run 5 seconds from now. That's obviously not what you want, but it's what you're asking for, so that's what you get.
In general, you can't do loops like this in event-based programming. What you need to do is turn the loop inside-out: each step does one iteration, then schedules the next call for the next one.
The fully-general transformation looks like this:
def runAnim():
iterwidth = iter(range(0, 200))
stepAnim(iterwidth)
def stepAnim(iterwidth):
try:
width = next(iterwidth)
except StopIteration:
return
test_label.config(width=width))
app.after(5000, stepAnim, iterwidth)
While that works for any iterable, when you're just iterating over numbers, it's usually a bit nicer to turn the for loop into an explicit counter, which is easier to invert. (Yes, that's the opposite of the "usual for instead of while and += 1 when you're not inverting things. The difference is that here, we can't access the magic of for or while, and while is a lot less magical, and therefore easier to invert.)
def runAnim():
stepAnim(0, 200):
def stepAnim(width, maxWidth):
test_label.config(width=width))
width += 1
if width < maxWidth:
app.after(5000, stepAnim, width, maxWidth)
However, in this particularly simple case, you might be able to get away with scheduling 200 callbacks, ranging from 5 to 1000 seconds into the future:
def runAnim():
for width in range(0, 200):
app.after(5000 * width, lambda width = width: test_label.config(width=width))
This might cause the timer to drift a lot more badly, or it might even choke up the scheduler and add lag to your program, but it's at least worth trying.
Speaking of drift:
Back at the start, I mentioned that after(5000, …) means 5 seconds after right now.
An after can fire a bit late. As the docs say: "Tkinter only guarantees that the callback will not be called earlier than that; if the system is busy, the actual delay may be much longer."
So, what happens if it fires after, say, 5.2 seconds? Then the second tick happens 5 seconds after that, at 10.2 seconds, not at 10 seconds. And if they're all firing a bit late, that adds up, so by the end, we could be 20 seconds behind.
Worse, what if after fires exactly at 5.0 seconds, but the Label.config takes 0.2 seconds to run? Then we're absolutely guaranteed to be 20 seconds behind. (Plus any additional error from after itself.)
If this matters, you need to keep track of the desired "next time", and wait until then, not until 5 seconds from whenever it is now. For example:
import datetime as dt
def runAnim():
stepAnim(0, 200, dt.datetime.now() + dt.timedelta(seconds=5):
def stepAnim(width, maxWidth, nextTick):
test_label.config(width=width))
width += 1
if width < maxWidth:
now = dt.datetime.now()
delay = (nextTick - now).total_seconds() * 1000
nextTick += dt.timedelta(seconds=5)
app.after(delay, stepAnim, width, maxWidth, nextTick)
Goal: I would like to see how many times python is able to print something per 1 second.
For educational purposes I'm trying to make a script that shows how many times per every second a random module will appear in a loop. How to do it in a fastest pythonic way?
At first, to count seconds I wrote this code:
import time
sec = 0
while True:
print(sec)
time.sleep(1)
sec += 1
But this one seems slower than a real seconds.
So I decided to use local seconds. Also, before continue my script I wanted to count how many times python will print 'you fool' manually, so I wrote following code:
import time
def LocalSeconds():
local_seconds = time.gmtime()[5:-3]
local_seconds = int(local_seconds[0])
return local_seconds
while True:
print(LocalSeconds(), 'you fool')
Output:
first second - 14 times per second;
next second - 13 times;
next second - 12 times, etc. Why it goes slower?
Where I end / stuck right now:
import time, random
def RandomNumbers():
return random.randint(3,100)
def LocalSeconds():
local_seconds = time.gmtime()[5:-3]
local_seconds = int(local_seconds[0])
return local_seconds
def LocalSecondsWithPing():
local_seconds_ping = time.gmtime()[5:-3]
local_seconds_ping = int(local_seconds[0:1])
return local_seconds_ping
record_seconds = []
record_seconds_with_ping = []
while True:
record_seconds.append(LocalSeconds())
record_seconds_with_ping.append(LocalSecondsWithPing())
if record_seconds == record_seconds_with_ping:
RandomNumbers()
del record_seconds_with_ping[0]
del record_seconds[-1]
Also, I guess I need to use "for" loop, not "while"? How to do this script?
Counting a single second won't give you a good result. The number of prints in a single second may vary depending on things like other threads currently running on your system (for the OS or other programs) and may be influenced by other unknown factor.
Consider the followind code:
import calendar
import time
NumOfSeconds=100 #Number of seconds we average over
msg='Display this message' #Message to be displayed
count=0 #Number of time message will end up being displayed
#Time since the epoch in seconds + time of seconds in which we count
EndTime=calendar.timegm(time.gmtime()) + NumOfSeconds
while calendar.timegm(time.gmtime())<EndTime: #While we are not at the end point yet
print(msg) #Print message
count=count+1 #Count message printed
print(float(count)/NumOfSeconds) #Average number of prints per second
Here calendar.timegm(time.gmtime()) gives us the time in seconds since the epoch (if you don't know what that is, read this. But basically it's just a fixed point in time most computer system now days use as a reference point.
So we set the EndTime to that point + the number of seconds we want to average over. Then, in a loop, we print the message we want to test and count the number of times we do that, between every iteration checking that we are not past the end time.
Finally we print the average number of times per seconds that we printed the message. This helps with the fact that we may end up start counting near the end of a whole second since the epoch, in which case we won't actually have a whole second to print messages, but just a fraction of that. If we make sure NumOfSeconds is large enough, that error addition will be small (for example, for NumOfSeconds=100 that error is ~1%).
We should note that the actual number would also depend on the fact that we are constantly testing the current time and counting the number of prints, however, while I haven't tested that here, it is usually the case that printing to the screen takes significantly longer time than those operations.
In my app I want to allow the user to scroll through images by holding down an arrow key. Not surprisingly with larger images the pc can't keep up, and builds up a potentially large buffer that carries on being processed after the key is released.
None of this is unexpected, and my normal answer is just to check the timestamp in the event against the current time and discard any events that are more than (say) .2 seconds old. This way the backlog can never get too large.
But tkinter uses some random timebase of events so that comparing with time.time() is meaningless, and I can't find a function to get hold of tkinter's own clock. I'm sure its in there, it's just most of the pythonised tkinter documentation is a bit naff, and searching for time or clock isn't helping either.
def plotprev(self,p):
if time.time() - p.time > .2:
return
Sadly this test always returns true, where is tkinter's pseudo clock to be found?
Any other method will be complex in comparison.
well it's nor very nice, but it isn't too tedious and seems to work quite well: (with a little bit of monitoring as well)
def checklag(self,p):
if self.lasteventtime is None: #assume first event arrives with no significant delay
self.lasteventtime = p.time
self.lasteventrealtime = time.time()
self.lagok=0
self.lagfail=0
return True
ptdiff = (p.time-self.lasteventtime) / 1000
rtdiff = time.time() - self.lasteventrealtime
lag = rtdiff-ptdiff
if lag < .3:
self.lagok += 1
if self.lagok %20 == 0:
print("lagy? OK: %d, fail: %d" %(self.lagok, self.lagfail))
return True
else:
self.lagfail += 1
return False
I try to use 'IF' in python in order to achieve the algorithm that can automatically ajust the value of a parameter in 'IF' according to some stock trasactions.
if self.sellcount==0 and int(time.time())-self.programstarttime>600:
if cur_sum/total_sum>0.15:
Other Code
else:
if cur_sum/total_sum>0.35:
Other Code
I try to achieve that if my algorithm do not sell any stock for 10 minutes, the algorithm can automatically change the condition from 0.35 to 0.15. However, the code above will change from 0.15 to 0.35 after selling stocks for one time. I want the code to keep 0.15 after selling stocks for one time.
I'd like to start with a disclaimer to be careful, stock trading is not this easy and you can lose a lot of money with a simple algorithm (just as you can with a complex one)
However, this is also a nice example to understand how to deal with running a program over time in Python and understanding conditional logic.
There are a few basic constructs you'll want to know for this. The first concept is that to keep track of time constantly in your program, you likely want to put your code in an infinite loop. That will keep your programming doing what you want until you are done. This can be done like this:
while True:
Now that you have this setup, we just need to keep track of time. This can be done easily by setting a variable and incrementing it by how long you wait between iterations. However, we still need to track time. Python has a nice sleep function implemented in the time module. This function causes your program to pause for a number of seconds that you desire and then resume going through the rest of your code.
from time import sleep
last_sold_stock_time = 0
wait_time = 1
while True:
# <Condition Code goes here>
# This is in seconds
sleep(wait_time)
# Keep track of how much time has passed.
last_sold_stock_time += wait_time
Now, you just need to change your condition value based on the time. The full code will probably end up looking something like this:
from time import sleep
# The number of seconds since last bought a stock, assumes start is 0
last_sold_stock_time = 0
# This is in seconds
wait_time = 1
# ten minutes in seconds
ten_minutes = 600
while True:
# Figure out these values however you do
cur_sum = 0
total_sum = 1
if last_sold_stock_time <= ten_minutes:
condition_value = 0.35
else:
condition_value = 0.15
if cur_sum/total_sum > condition_value:
# Do something
pass
sleep(wait_time)
last_sold_stock_time += wait_time
Developing a process that should read data at consistent intervals. The time period to read data varies depending on the network. I thought this should be straightforward but I can never get consistent timing. Looking for a more consistent and stable system that responds well to network speed variability.
currently I am using a model that follows
|<--read data-->|<--post process-->|<--sleep x seconds to maintain period-->|
|<------------------------------known data rate---------------------------->|
My code does something like
data_rate = 5 # Hz
while 1:
# read in data
rd_start = time.time()
data = getdata()
rd_stop = time.time()
# Post processing
pp_start = time.time()
rate = 1.0/(rd_start - oldstart) if oldstart else data_rate
old_start = rd_start
print rate
post_process(data)
pp_stop = time.time()
sleep_time = 1.0/data_rate - ((rd_stop-rd_start) + (pp_stop-pp_start))
sleep_time = sleep_time if sleep_time>0 else 0
time.sleep(sleep_time)
I also have some logic that changes the update rate (data_rate) if the network is having trouble meeting that speed (sleep times are consistently negative) but that is working correctly.
For some reason my data rate is never consistent (And runs at about 4.92 Hz when it stabilizes). Also this method is pretty unstable. What is the better way to do this? Threading.Timers() comes to mind?
Could the consistent offset in frequency be caused by errors with time.sleep()?
How accurate is python's time.sleep()?