3

How to extract data point at mouse click in PySimpleGUI?

I have a collection of 2D (x,y) data points. I have plotted the 2D data using PySimpleGUI graph element and drawCircle function. Now, when I click on one circle (which represents one data point), I want to execute a function that takes this (x,y) data point as its input. I want to get the exact (x,y) data point that I plotted (not the coordinates of the location that I click) when I click anywhere on the circle.

import PySimpleGUI as sg

layout = [[sg.Graph(canvas_size=(800, 800), graph_bottom_left=
(-105, -105), graph_top_right=(105, 105), background_color='white',
                    key='graph', tooltip=None, enable_events=True)], ]
window = sg.Window('Network Prediction Evaluation', layout,
                   grab_anywhere=True).Finalize()
graph = window['graph']

# Draw axis
graph.DrawLine((-100, 0), (100, 0))
graph.DrawLine((0, -100), (0, 100))
for x in range(-100, 101, 20):
    graph.DrawLine((x, -3), (x, 3))
    if x != 0:
        graph.DrawText(x, (x, -10), color='green')
for y in range(-100, 101, 20):
    graph.DrawLine((-3, y), (3, y))
    if y != 0:
        graph.DrawText(y, (-10, y), color='blue')

# Draw Graph
f = open("./data/train_analysis.txt", "r")
s_id = []
true_val = []
pred_val = []
f.readline()  # to remove column names
for x in f:
    s = x.split()
    s_id.append(float(s[0]))
    true_val.append(int(round(float(s[1]) * 100)))
    pred_val.append(int(round(float(s[2]) * 100)))

for i in range(len(true_val)):
    # graph.DrawLine((true_val[i],0),(true_val[i],pred_val[i]))
    graph.DrawCircle((true_val[i], pred_val[i]), 4, line_color='red',
                     fill_color='blue')

while True:
    event, values = window.read()
    if event is None:
        break
    val = values[event]
    print(val)


I am able to get the coordinates of the location of the mouse click. But I want to get the exact data point when I click anywhere on the circle. How can I do that?

matan h
  • 900
  • 1
  • 10
  • 19
Vandana Rajan
  • 161
  • 1
  • 12
  • 1
    Ooops... misread the question. You will have to determine yourself what you click on. There's nothing that I know of that will tell you if you've clicked on a drawn circle or any other figure. – Mike from PSG Nov 12 '19 at 17:23

0 Answers0