able to start the time count by clicking LMB but could not find a way to stop and reset it.
import PySimpleGUI as sg
import time
from pynput import mouse
sg.theme('DefaultNoMoreNagging')
layout = [[sg.Text('0.0', size=(10, 1), justification='center', font=('Helvetica', 48), key='-TIMER-')]]
window = sg.Window('Timer', layout, finalize=True)
start_time = 0
timer_running = False
def on_click(x, y, button, pressed):
global start_time, timer_running
if button == mouse.Button.left:
if pressed:
if not timer_running:
start_time = time.time()
timer_running = True
else:
if timer_running:
elapsed_time = round(time.time() - start_time, 1)
window['-TIMER-'].update('0.0')
timer_running = False
listener = mouse.Listener(on_click=on_click)
listener.start()
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
listener.stop()
break
window.close()
What I want is said in title, for example LMB click (and hold) somewhere on the screen, the timer should start as soon as I click, then reset to 0.0s and stop as LMB release.
By the above code the count can start but cannot be stopped nor reset. Can’t find a shortest or cleanest way to do the ‘reset and stop’ work, don’t know if I do the ‘reset and stop’ wrong or the monitoring part of releasing LMB wrong. Newbie to coding and thank you very much!
Elysion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.