P
P
Petr Kolesnikov2021-03-08 13:13:18
Python
Petr Kolesnikov, 2021-03-08 13:13:18

How to make the press times between start and stop buttons?

class IncrediblyCrudeClock(Label):
def update(self, *args):
self.text = time.asctime()
def start(self):
Clock.schedule_interval(self.tick,1)

timepassed = 0

def countsecond(dt):
global timepassed
timepassed += 1


layout = BoxLayout(padding = 100, orientation='vertical')
button_start = Button(text = "start")
button_stop = Button(text = "stop")
crudeclock = IncrediblyCrudeClock()
Clock.schedule_interval(crudeclock. update, 1)
label = Label(text = str(timepassed))
event = Clock.schedule_interval(countsecond, 1)
Clock.unschedule(event)
currentline = BoxLayout()

def showtime(instance):
global currentline
currentline = BoxLayout()
timestart = IncrediblyCrudeClock()
Clock.schedule_once(timestart.update)
currentline.add_widget(timestart)
timepassed = 0
event()
layout.remove_widget(instance)
layout.add_widget(button_stop)
layout.add_widget(currentline)

def hidetime(instance):
Clock.unschedule(event)
global label
label = Label(text = str(timepassed))
global currentline
timestop = IncrediblyCrudeClock()
Clock.schedule_once(timestop.update)
currentline.add_widget( timestop)
currentline.add_widget(label)
line = currentline
layout.remove_widget(currentline)
layout.remove_widget(instance)
layout.add_widget(button_start) layout.add_widget (
line)


class ) crudeclock = IncrediblyCrudeClock() Clock.schedule_interval(crudeclock.update, 1) button_start.bind(on_press = showtime) button_stop.bind(on_press = hidetime) return layout

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RusiDev, 2021-03-08
@RusiDev

time.time()
Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.
On the start button, do: start_time = time.time()
On the finish button, set: end_time = time.time()
And then, at the finish, subtract the end time minus the start time:
end_time - start_time

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question