0

Wondering how to bind an event to the left mouse button but not having to be hovered in the window/app/root. Basically, I want the event to happen where-ever my mouse pointer happens to be on my screen. Right now I can only figure out how to make the mouse button bindings work when the mouse is hovered on the window/app/root.

    from tkinter import*
    from tkinter import Tk, Label, StringVar
    import random
    import pyautogui

    root = Tk()
    root.configure(background='black')
    root.attributes("-topmost", True)
    root.overrideredirect(True)
    root.geometry("100x100")

    title_bar = Frame(root, bg='black')
    title_bar.pack(side=TOP, fill=X)

    tout = StringVar()
    label = Label(root, textvariable=tout, font=('TkDefaultFont', 
    50), bg='black', fg='red')
    label.pack()

    def _quit():
        root.quit()
        root.destroy()

    def move_app(event):
        root.geometry(f'+{event.x_root}+{event.y_root}')

    def gen_rand(event):
        randvar = random.randint(0,99)
        tout.set(randvar)

    def clear_rand(event):
        tout.set("")

    myButton = Button(title_bar, text="X", font=('TkDefaultFont', 5), 
    bg='black', activebackground='red', fg='red', command=_quit)
    myButton.pack(side=RIGHT)

    root.bind('<Button-1>', gen_rand)
    root.bind('<Button-3>', clear_rand)
    root.bind("<B2-Motion>", move_app)

    root.mainloop()

2 Answers2

0

You cannot do this with tkinter, you'll have to find some platform-specific way to do it. Tkinter can only process events when it is the application in focus.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

This is not something that can be done with Tkinter, but rather Pyautogui to generate an event

pyautogui.click(x=100, y=200)  # move to 100, 200, then click the left mouse button.

if you wish to read an event you must use other module such as pynput How to know if the left mouse click is pressed

from pynput import mouse

def on_click(x, y, button, pressed):
    if button == mouse.Button.left:
        print('{} at {}'.format('Pressed Left Click' if pressed else 'Released Left Click', (x, y)))
        return False # Returning False if you need to stop the program when Left clicked.
    else:
        print('{} at {}'.format('Pressed Right Click' if pressed else 'Released Right Click', (x, y)))

listener = mouse.Listener(on_click=on_click)
listener.start()
listener.join()```

  [1]: https://pypi.org/project/PyAutoGUI/
  [2]: https://stackoverflow.com/questions/39235454/how-to-know-if-the-left-mouse-click-is-pressed
DanielGzgzz
  • 118
  • 1
  • 6
  • Thank you for your response. I seem to still be stuck. I installed pyautogui and i imported it but not sure how the code should look. I just posted the code in the original question – Vincinerator Jan 13 '22 at 19:19
  • Oh my mistake , pyautogui could only make an event if you want to listen to events you must use other module such as pynput. I will edit my answer – DanielGzgzz Jan 13 '22 at 19:43
  • Thank you for all your help. Im so new to this im still lost. I just want my random number generator to change numbers by left clicking no matter where the cursor is. – Vincinerator Jan 13 '22 at 20:22