Code Monkey home page Code Monkey logo

Comments (4)

jason990420 avatar jason990420 commented on May 28, 2024 1

There's no method to move the pointer cursor in PySimpleGUI, but it can be done by tkinter code after window finalized.

window.TKroot.event_generate('<Motion>', warp=True, x=x, y=y)

It is a little complex to convert between tkinter Canvas coords and user's coordinates.

Demo Code

import PySimpleGUI as sg


def draw_line(x0, y0, x1, y1):
    x2, y2 = graph._convert_canvas_xy_to_xy(x1, y1)
    window.TKroot.event_generate('<Motion>', warp=True, x=x2+dx, y=y2+dy)
    graph.draw_line((x0, y0), (x1, y1), color='white', width=5)

size = xmax, ymax = (500, 500)
width, height = (400, 400)

layout = [
    [sg.Graph(size, (0, xmax), (ymax, 0), pad=(0, 0), background_color='Green', key="Graph")],
    [sg.Push(), sg.Button("Draw"), sg.Push()],
]

window = sg.Window("Cursor location", layout, margins=(0, 0), finalize=True)
graph = window['Graph']
draw, x0, y0 = False, None, None
dx, dy = graph.widget.winfo_x(), graph.widget.winfo_y()

while True:

    event, values = window.read(timeout=50)

    if event == sg.WIN_CLOSED:
        break

    elif event == "Draw":
        draw, x0, y0, x1, y1 = True, 50, 250, 50, 250
        graph.erase()

    elif event == sg.TIMEOUT_EVENT and draw:
        x1 += 10
        draw_line(x0, y0, x1, y1)
        if x1 >= 450:
            draw, x0, y0 = False, None, None
        else:
            x0, y0 = x1, y1

window.close()

python_wimofPjOpi

from pysimplegui.

camilabbertelli avatar camilabbertelli commented on May 28, 2024

I don't know if I've done something wrong, but when I click 'Draw' the pointer just stays put. The line is drawn, but the cursor doesn't move.

from pysimplegui.

jason990420 avatar jason990420 commented on May 28, 2024

It maybe "Platform dependent", so work on my WIN10, but not work on your Linux.

Here with the library pyautogui

import pyautogui
import PySimpleGUI as sg


layout0 = [
    [sg.Button(f'({x:0>3d}, {y:0>3d})', key=(x, y), font=("Courier New", 10))
        for x in range(100, 1000, 200)]
            for y in range(50, 200, 50)
]
layout = [
    [sg.Graph((1000, 200), (0, 200), (1000, 0), pad=(0, 0), background_color="green", key="Graph")],
    [sg.Push(), sg.Column(layout0), sg.Push()],
]
window = sg.Window("Cursor location", layout, margins=(0, 0), finalize=True)
graph = window['Graph']
figure = None

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif isinstance(event, tuple):
        x1, y1 = event
        x2, y2 = graph._convert_canvas_xy_to_xy(x1, y1)
        dx, dy = graph.widget.winfo_rootx(), graph.widget.winfo_rooty()
        pyautogui.moveTo(x2+dx, y2+dy)
        if figure:
            graph.delete_figure(figure)
        figure = graph.draw_text(f"({x1}, {y1})", (x1, y1), color='white')

window.close()

python_3SIVoUG63h

from pysimplegui.

camilabbertelli avatar camilabbertelli commented on May 28, 2024

Yeah, I think it is, still not working, thanks anyway 👍

from pysimplegui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.