[Python] How to get accurate timing with PyQT

Discussão em 'Python' iniciado por Stack, Agosto 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to build an app that will accurately count time between taps, like a tap function on a metronome but finding the results to be inaccurate;

    current_time = QDateTime.currentMSecsSinceEpoch ()

    tap_old = tap_new
    tap_new = current_time
    tap_delay = (tap_new - tap_old)
    bpm = 60 / tap_delay


    This was the plan but it didn't work out how I expected, giving me a range of values from around 110bpm to 130bpm while tapping along with another metronome set to 120bpm.

    I've since tried adding the seconds and the last 3 digits of the microseconds together to see if this increased accuracy, but to no avail. And yes I'm sure this can be done more efficiently, but it was just a test;


    current_sec = QDateTime.currentSecsSinceEpoch ()
    current_msec = QDateTime.currentMSecsSinceEpoch ()
    current_msec = str(current_msec)
    current_msec1 = current_msec[10]
    current_msec2 = current_msec[11]
    current_msec3 = current_msec[12]
    current_msec = (current_msec1 + current_msec2 + current_msec3)
    current_msec = int(current_msec)
    current_msec = current_msec / 1000
    current_time = current_sec + (current_msec)


    I've tried just subtracting the times by using current_time = QTime.currentTime() and also trying to compare the two; tap_delay = QTime.mSecsTo(self, tap_old). I definitely don't know how to use these functions so they just gave me errors.

    Continue reading...

Compartilhe esta Página