一个点击计数器,显示您使用 python 3.8 、 tkinter 、 win32 和 pynput 点击左侧 mb 的次数

a click counter that displays how many times you clicked left mb using python 3.8 , tkinter , win32 and pynput

这是最基本的示例。我从这个网站找到了显示脚本,并从 pynput 的纪录片中复制了鼠标侦听器。当我删除计数器部分时,它成功显示了 clickamount 变量的值,但是当我将它添加回去时,它不显示任何内容,也没有给出任何错误消息

#import things

from pynput.mouse import Listener
import tkinter, win32api, win32con, pywintypes

#variable defining

clickamount=0

#counter part using pynput

with Listener() as listener:
    listener.join()
def on_click(button, pressed):
    clickamount+=1

#display the amount

label = tkinter.Label(text=clickamount, font=('Times New Roman','80'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+0+0")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")

hWindow = pywintypes.HANDLE(int(label.master.frame(), 16))
# http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
# The WS_EX_TRANSPARENT flag makes events (like mouse clicks) fall through the window.
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)

label.pack()
label.mainloop()

只需使用 tkinter 的内置事件和绑定:

clickamount = 0

def onclick(event):
    global clickamount
    clickamount+=1

root = Tk()
root.bind("<Button-1>", onclick)
root.mainloop()

鼠标方法:

import mouse

clickamount = 0

def onclick():
    global clickamount
    clickamount+=1
    print(clickamount)

root = Tk()
mouse.on_click(onclick)
root.mainloop()