#Discord.py: if I press the start button and then the stop button, how can I calculate the time elaps
103 messages · Page 1 of 1 (latest)
personally, i would subclass your view, then in the callback of the start button, store the current time as start_time, then in the stop button callback, store stop_time as the current time, then subtract start time from the end time
@wispy torrent ^^
How can I memorize it?
i would use time.now()
import time
start = time.time()
elapsed = time.time() - start
Would this work?
wait, I'll try
There are more accurate, higher resolution methods, for debugging but if you want to measure things in 100ms or seconds then this is perfect
In hour ?
Maybe doing the conversion from seconds to minutes and minutes to hours would work?
Yeah sure will work perfectly. You're basically getting two separate times from the clock and subtracting them to find the elapsed time
Yes youll have to convert the result if you want to know the value in
H M S format
Now I'll try, can you wait 2 seconds here?
show me the code for stop_time and start_time
the start_time variable is declared inside one function, so it remains in that functions local scope table meaning it cannot be accessed outside that function. You can either make it a global variable, or try to pass it into fine()
How can I create a global variable?
just declare it outside inizio() then assign it a value inside inizio. Ofc you cannot simply declare variables in python, so you need to initialise it with whatever value
So like:
start_time = "nothing"
# Code here
async def inizio(...):
global start_time
start_time = time.time()
# Code here
Don't forget to tell the interpreter that it's a global variable, when you're inside inizio()
let me see
This
Revert code back to what it was in screenshot.
In inizio() change self.start_time to SimpleView2.start_time and the same for stop_time
If this doesnt work 💀
@wispy torrent
stop time wouldnt be the current time - start
stop time would be the current time, then the elapsed time would be the stop_time - start_time
and do not use global here
yeah no global. didnt see it was a class
inside or outside the init?
Okok
basically remove the global lines
keep the self.start/stop_time in init, no other declarations
inside inizio() and fine(), change self to class name
wait
Im confused
and stupid
What should I do here?
@open flame @tranquil lion ?
Above channel = bot.get_token..., add SimpleView2.start_time = time.time()
@wispy torrent
this is really wrong
your editing things that dont need to be edited
do you know OOP?
things in your fine callback are still wrong
you dont use SimpleView2, you should use self
you have done it multiple times aswell
I changed SimpleView2.stop/start_time
In Init, is it ok now?
@wispy torrent
For safety reasons we do not allow files with certain file extensions.
You can share your code using triple backticks like this:
```
YOUR CODE
```
For longer scripts use Hastebin or GitHub Gists and share the link here
- ada.txt
There is no error, write the number in the message, are they seconds and milliseconds?
seconds
10 is 10 seconds
Okok
I counted the seconds and they are correct, it works
Now I have to convert the elapsed seconds into minutes and hours too
its ok dw about it
you can calculate the hours and minutes separately using the % operator, or use timedelta, which i'll post in a minute
from datetime import timedelta
time_d = timedelta(seconds=int(elapsed_time))
Did you get the result you wanted?
I didn't understand how to do it, sorry
Add
from datetime import timedelta
at the top where you import things.
Then, under the elapsed_time initialisation, add
time_d = timedelta(seconds=int(elapsed_time))
actually wait
dont name it time
This should return HH:MM:SS
yes