-2

Here is the very basic example . I've found the displaying script from this website and copied the mouse listener from pynput's documentary . When i remove the counter part it succesfully displays the value of clickamount variable but when i add it back it doesnt display anything also it doesnt give any error message

#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()

1 Answers1

0

Just use tkinter's inbuilt events and bindings:

clickamount = 0

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

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

Method with mouse:

import mouse

clickamount = 0

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

root = Tk()
mouse.on_click(onclick)
root.mainloop()
TheFluffDragon9
  • 514
  • 5
  • 11
  • But that works only if i click on the tkinter window isn't it? – Hulusi Bumin Yalçın May 05 '20 at 10:15
  • @HulusiBuminYalçın That is true. If you want to detect click anywhere, I would recommend the mouse module (`pip install mouse`) it is very easy to use and is very small in size. I have updated my answer to include the new mouse code. You can find a very useful list of all of `mouse`'s capabilities [here](https://github.com/boppreh/mouse) – TheFluffDragon9 May 05 '20 at 11:54
  • **@TheFluffDragon9** dude**thanks** for all your support i've successfully finished my code and it works but i am curious about can i use the same package **(mouse)** to detect **right mouse button clicks** for resetting the variable **clickcount**? – Hulusi Bumin Yalçın May 06 '20 at 04:09
  • Yeah you can using either `mouse.on_right_click(callback, args=())` to invoke a callback or `mouse.is_pressed(button='right')` which returns a boolean. There is also `mouse.on_button(callback, args=(), buttons=('left', 'middle', 'right', 'x', 'x2'), types=('up', 'down', 'double'))` which can be used if you want to detect anything more complicated. Glad I could help! – TheFluffDragon9 May 06 '20 at 13:59