如何在PySimpleGUI中用鼠标单击从GUI访问绘制的数据值?

时间:2019-11-11 14:48:11

标签: graph pysimplegui

如何在PySimpleGUI中单击鼠标提取数据点?

我有2D(x,y)数据点的集合。我已经使用PySimpleGUI图形元素和drawCircle函数绘制了2D数据。现在,当我单击一个圆圈(代表一个数据点)时,我想执行一个以该(x,y)数据点作为输入的函数。当我在圆上的任意位置单击时,我想获取绘制的精确(x,y)数据点(而不是单击位置的坐标)。

import math
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)

我能够获得鼠标单击位置的坐标。但是,当我在圆上的任意位置单击时,我想获得确切的数据点。我该怎么办?

0 个答案:

没有答案