1

I'm trying to get the mouse coordinates, with pynput, and see it with a GUI with PySimpleGUI but I am getting a lot of problens, and I don't know how can I do this.

I want to show the mouse coordinates at the "x - y" string below

import PySimpleGUI as sg
from pynput import mouse

sg.theme("DarkAmber")

layout = [[sg.Text("Mouse Coord")],
          [sg.Text("x - y")]]

window = sg.Window("Mouse Coord 1.0", layout, keep_on_top = True)

while True:
    event, values = window.read()
    print(event, values)

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

window.close()
  • "but I am getting a lot of problens" please explain in detail all the problems that you are facing so we can help you better. Paste all error messages (if any) in full and in plaintext – Caridorc Feb 19 '23 at 23:55
  • Because PySimpleGUI does not support mouse move events, it's much easier to do that with other GUI frameworks such as tkinter or QT. Anyway if you insist PySimpleGUI, use ```window.TKroot``` and bind the '' event on that. See [this question](https://stackoverflow.com/questions/22925599/mouse-position-python-tkinter). – relent95 Feb 20 '23 at 01:39

2 Answers2

2

Not sure why you need the mouse coordinate, following code demo the way to get it by PySimpleGUI.

import PySimpleGUI as sg

sg.theme("DarkAmber")

layout = [[sg.Text("Mouse Coord:"), sg.Text(size=20, key='Coordinate')]]
window = sg.Window("Mouse Coord", layout, finalize=True)
window.bind('<Motion>', 'Motion')

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Motion':
        e = window.user_bind_event
        window['Coordinate'].update(f'({e.x}, {e.y})')

window.close()

enter image description here

You can get the screen coordinate by e.x_root and e.y_root, like

        window['Coordinate'].update(f'({e.x_root}, {e.y_root})')

The '<Motion>' in tkinter only work when mouse on the tkinter window, following code demo the way using pynput library to capture the motion event, then pass it to PySimpleGUI.

from pynput.mouse import Listener
import PySimpleGUI as sg


def on_move(x, y):
    window.write_event_value('Motion', (x, y))

sg.theme("DarkAmber")

layout = [[sg.Text("Mouse Coord:"), sg.Text(size=20, key='Coordinate')]]
window = sg.Window("Mouse Coord", layout, finalize=True, enable_close_attempted_event=True)

listener = Listener(on_move=on_move)
listener.start()

while True:

    event, values = window.read()

    if event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT:
        listener.stop()
        break
    elif event == 'Motion':
        x, y = values[event]
        window['Coordinate'].update(f'({x}, {y})')

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • The code will help me to get the position, to automate the clicks on PySimpleGUI, this is working but it only takes the coordinate on top of the window, I need to get the coordinate on the screen. – Gabriel Garcia Feb 20 '23 at 20:40
  • Update as above. – Jason Yang Feb 21 '23 at 01:59
  • If using the `enable_window_config_events` parameter when making the window, no need for bind. `Window.current_location` provides the location of the window on the screen. `mouse_location` gives the mouse location on the screen. I don't think any special tkinter code is needed. Should be able to do using plain PySimpleGUI code. EDIT - oh, except need something to get mouse movement events so that bind is needed. After that you can get the mouse location using a standard call. – Mike from PSG Feb 21 '23 at 15:19
  • IMO, this question is not about the option `enable_window_config_events` and binding is required, or there will be no event generated for mouse motion. – Jason Yang Feb 21 '23 at 15:28
0

It are giving the window coordinates but I need the window coordinates

import PySimpleGUI as sg
import pyautogui

position = pyautogui.position()

sg.theme("DarkAmber")

layout = [[sg.Text("Mouse Coord:"), sg.Text(size = 20, key = "Coordinate")]]
window = sg.Window("Mouse Coord", layout, finalize = True, keep_on_top = True)

window.bind("<Motion>", "Motion")

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == "Motion":
        e = window.user_bind_event
        window["Coordinate"].update(f"({e.x}, {e.y})")

window.close()

It return the window mouse coordinate