首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每秒点击量测试仪即使在休眠期间也会计算点击量

每秒点击量测试仪即使在休眠期间也会计算点击量
EN

Stack Overflow用户
提问于 2021-05-03 17:32:26
回答 1查看 64关注 0票数 0

我想写一个点击速度测试器,它在一开始工作得很好。现在,我希望您有一个特定的时间窗口,您可以在其中单击,然后显示最终结果。它将等待2秒,然后应该重新开始。

问题是,当它重新开始时,它还会计算您在2秒暂停中执行的点击量。我该如何解决这个问题呢?

代码语言:javascript
复制
#!/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()
EN

回答 1

Stack Overflow用户

发布于 2021-05-03 20:55:38

sleep的问题在于,它不能阻止事件的发生,但它会禁用程序中的事件处理。

正如我在评论中提到的,tkinter是构建这样一个点击计数器的有效选择,而且它也带来了python带来的好处,所以不需要第三方包。计数和显示阶段之间的转换是由一种“定时器”完成的。

因此,这可能是您的点击率计数器程序的一个很好的起点:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67366569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档