0

I want to write a click speed tester which worked fine at first. Now I want you to have a certain time window in which you can click, then the final results are displayed. It will wait 2 seconds and then it should start all over again.

The problem is that when it starts over again, it also counts the clicks you performed in the 2-second pause. How can I fix this?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from time import sleep
import time
from pynput import mouse
import os

CPSL=0
CPSR=0
Time=5
mode="Time"#oneDrack, Time, Double
startTime=0
nowTime=0
CPSLT=0
CPSRT=0
double=0
buttosPressed=0

def on_click(x, y, button, pressed):
    global CPSL, CPSR, mode, startTime, nowTime, double, Time, buttosPressed
    if (str(button) == "Button.left" and pressed):
        buttosPressed="CPSL"
        CPSL+=1
    if (str(button) == "Button.right" and pressed):
        buttosPressed="CPSR"
        CPSR+=1
    if (mode == "Time"):
        if (pressed):
            if double==0:
                print("start")
                CPSR=0
                CPSL=0
                if (buttosPressed=="CPSL"): CPSL=1
                else: CPSL=0
                if (buttosPressed=="CPSR"): CPSR=1
                else: CPSR=0
                print(CPSL, end=" ")
                print(CPSR)
                double=1
                startTime=time.time()
            else:
                nowTime=time.time()
                difTime=nowTime - startTime
                if (difTime < Time):
                    print(CPSL, end=" ")
                    print(CPSR)
                else:
                    if (buttosPressed=="CPSL"): CPSL-=1
                    if (buttosPressed=="CPSR"): CPSR-=1
                    print("Finaly")
                    print(CPSL, end=" ")
                    print(CPSR)
                    sleep (2.5)
                    double=0
with mouse.Listener(
        on_click=on_click
) as listener:
    listener.join()
Wolf
  • 9,679
  • 7
  • 62
  • 108
Nulli
  • 11
  • 2

1 Answers1

1

The problem with sleep is that it doesn't prevent events from happening but it disables event handling in your program. That's why you observe a lot of events after each sleep period.

As mentioned in my comment, tkinter is a valid option to build such a click counter, and it also brings the benefit to come along with python, so no need for 3rd party packages. The change between the stages counting and displaying is done by a sort of "timer". Actually, it's a timed function call by using the after method which is universal for all widgets.

So this could be a good starting point for you click-rate counter program:

import tkinter as tk

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.counting = False;
        self.clicks = 0
        self.label = tk.Label(text="", )
        self.label.bind('<Button-1>', self.label_click_left)
        self.label.pack()
        self.update_clock()
        self.root.mainloop()

    def label_click_left(self, event):
        if self.counting:
            self.clicks += 1
            
    def update_clock(self):
        if self.counting:
            self.label.configure(text='counting...')
        else:
            self.label.configure(text=f'{(self.clicks/2):.2f} CPS')
        self.counting = not self.counting
        self.root.after(2000, self.update_clock)

app=App()
Wolf
  • 9,679
  • 7
  • 62
  • 108