如何使tkinter Main循环等待matplotlib单击事件

2024-04-26 11:04:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Tkinter和matplotlib构建带有嵌入式绘图的GUI。我已经在我的窗口中嵌入了一个图形,现在希望使用matplotlib的事件处理程序从图形中获取两组x,y坐标,然后使用这些坐标创建一条直线,该直线从图形中的数据中减去。代码的简化版本如下所示:

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import tkinter as tk

#ideally this uses matplotlib's event handler and also waits for a click before registering the cooridnates
def choose_points():
    points = []
    window.bind("<Button-1>", on_click)
    points.append(graph_xy)
    window.bind("<Button-1>", on_click)
    points.append(graph_xy)
    return points

def on_click(event):
    window.unbind("<Button-1")
    window.config(cursor="arrow")
    graph_xy[0]=event.x
    graph_xy[1]=event.y

def line(x1=0,y1=0,x2=1,y2=1000):
    m=(y2-y1)/(x2-x1)
    c=y2-m*x2
    line_data=[]
    for val in range(0,20):
        line_data.append(val*m + c)
    return line_data

def build_line():
    points = []
    points = choose_points()
    #store line in line_list
    line_list=line(points[0],points[1],points[2],points[3])

#lists needed
line_list=[]
graph_xy=[0,0]

#GUI
window=tk.Tk()
window.title("IPES Graphing Tool")
window.geometry('1150x840')

#Make a frame for the graph
plot_frame = tk.Frame(window)
plot_frame.pack(side = tk.TOP,padx=5,pady=5)

#Button for making the straight line
line_btn = ttk.Button(plot_frame,text="Build line", command = build_line)
line_btn.grid(row=4, column=2,sticky='w')

#make empty figure
fig1=plt.figure(figsize=(9,7))
ax= fig1.add_axes([0.1,0.1,0.65,0.75])

#embed matplotlib figure
canvas = FigureCanvasTkAgg(fig1, plot_frame)
mpl_canvas=canvas.get_tk_widget()
canvas.get_tk_widget().pack(padx=20,side=tk.BOTTOM, fill=tk.BOTH, expand=False)
toolbar = NavigationToolbar2Tk(canvas, plot_frame)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=False)

window.mainloop()

显然,此示例不会以任何方式绘制或使用直线,坐标也不正确,因为它们不会转换为图形的坐标。我尝试用plt.connect('button_press_event',on_click)替换window.bind("<Button-1>",wait_click),但这并不等待单击,因此出现错误,因为程序尝试访问points,但它是空的

我想使用matplotlib事件处理的功能,这样我就可以使用event.xdataevent.inaxes等方法来避免不必要的额外工作

多谢各位


Tags: importevent图形plotmatplotliblinebuttonwindow
2条回答

因此,我通过调整@Henry Yik的答案,获得了我想要的功能。这个问题是通过简单地使用canvas.mpl_disconnect(cid)解决的

我基本上使用了一个按钮来使用一个名为choose_cords()的函数,该函数将接收一个名为draw的对象,该对象的类型为DrawLine,包含x和y坐标来构建直线。然后,函数将发出命令canvas.mpl_connect('button_press_event', draw.get_cords),开始侦听图形上的单击。注册两次单击后,matplotlib事件处理程序将从draw对象中断开连接。代码是这样的


import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import tkinter as tk

window=tk.Tk()
window.title("IPES Graphing Tool")
window.geometry('1150x840')

plot_frame = tk.Frame(window)
plot_frame.pack(side = tk.TOP,padx=5,pady=5)

fig1=Figure(figsize=(9,7))
ax= fig1.add_axes([0.1,0.1,0.65,0.75])

canvas = FigureCanvasTkAgg(fig1, window)
canvas.get_tk_widget().pack(padx=20,side=tk.TOP, fill=tk.BOTH, expand=False)
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()

def choose_cords(draw):
    draw.cid=canvas.mpl_connect('button_press_event', draw.get_cords)

class DrawLine: # a simple class to store previous cords
    def __init__(self):
        self.x = None
        self.y = None
        self.cid = None

    def get_cords(self, event):
        if self.x and self.y:
            ax.plot([self.x, event.xdata], [self.y, event.ydata])
            canvas.draw_idle()
            canvas.mpl_disconnect(self.cid)
            self.__init__()
            return
        self.x, self.y = event.xdata, event.ydata

draw = DrawLine()
draw_btn = tk.Button(window, text="Draw the Line", command=lambda:choose_cords(draw)).pack(side=tk.TOP,padx=5,pady=5)

window.mainloop()

我在绘制直线后添加了一个回车,因为我每次都想要一条新线,而不是一个连续线

再次感谢亨利的回答,这是你的答案

您应该使用canvas.mpl_connect来触发events,然后检索xdataydata来绘制线。请参见下面的示例:

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import tkinter as tk

window=tk.Tk()
window.title("IPES Graphing Tool")
window.geometry('1150x840')

plot_frame = tk.Frame(window)
plot_frame.pack(side = tk.TOP,padx=5,pady=5)

fig1=Figure(figsize=(9,7))
ax= fig1.add_axes([0.1,0.1,0.65,0.75])

canvas = FigureCanvasTkAgg(fig1, window)
canvas.get_tk_widget().pack(padx=20,side=tk.TOP, fill=tk.BOTH, expand=False)
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()

class DrawLine: # a simple class to store previous cords
    def __init__(self):
        self.x = None
        self.y = None

    def get_cords(self, event):
        if self.x and self.y:
            ax.plot([self.x, event.xdata], [self.y, event.ydata])
            canvas.draw_idle()
        self.x, self.y = event.xdata, event.ydata

draw = DrawLine()
canvas.mpl_connect('button_press_event', draw.get_cords)

window.mainloop()

相关问题 更多 >