我想写一个点击速度测试器,它在一开始工作得很好。现在,我希望您有一个特定的时间窗口,您可以在其中单击,然后显示最终结果。它将等待2秒,然后应该重新开始。
问题是,当它重新开始时,它还会计算您在2秒暂停中执行的点击量。我该如何解决这个问题呢?
#!/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()发布于 2021-05-03 20:55:38
sleep的问题在于,它不能阻止事件的发生,但它会禁用程序中的事件处理。
正如我在评论中提到的,tkinter是构建这样一个点击计数器的有效选择,而且它也带来了python带来的好处,所以不需要第三方包。计数和显示阶段之间的转换是由一种“定时器”完成的。
因此,这可能是您的点击率计数器程序的一个很好的起点:
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()https://stackoverflow.com/questions/67366569
复制相似问题