【发布时间】:2021-07-25 17:25:10
【问题描述】:
我想写一个一开始运行良好的点击速度测试器。现在我希望您有一个特定的时间窗口,您可以在其中单击,然后显示最终结果。它将等待 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()
【问题讨论】:
-
我猜
sleep在这里不合适。用一个计时器来代替开始和停止测量会话呢? -
我不明白我该怎么做这样的计时器?
-
也许这会帮助你理解 tkinter 选项stackoverflow.com/a/2401181/2932052
-
好的,我去看看 THX。
标签: python python-3.x mouseevent sleep