显示实时更新绘图时出现Tcl\u AsyncDelete错误

2024-04-25 17:54:45 发布

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

我想在Tkinter窗口上显示一个每秒更新一次的绘图(和其他东西一起)。我只需要从数据矩阵中得到一行并绘制它,然后转到下一行,依此类推。你知道吗

因为我需要一个启动/停止按钮,所以我使用threading。你知道吗

为了做到这一点,我遵循了this post,它基本上满足了我的需要。你知道吗

但是,一段时间后,Python崩溃,Spyder显示以下错误:

An error occurred while starting the kernel
Tcl_AsyncDelete: async handler deleted by the wrong thread

我试着读了一些关于它的文章,但是我没有找到一个解决办法或者解释。你知道吗

下面是一个示例代码:

import tkinter as tk
import numpy as np

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import time
import threading

continuePlotting = False
line = 0
data = np.random.rand(100, 500)

def change_state():
    global continuePlotting
    if continuePlotting == True:
        continuePlotting = False
    else:
        continuePlotting = True

def data_points():
    global line
    global data

    l = line % len(data) - 1
    r = data[l]

    line = line+1

    return r

def app():
    root = tk.Tk()
    root.configure(background='white')
    # First Plot
    top = tk.Frame(root)
    top.pack(fill='both')

    fig = Figure()
    ax = fig.add_subplot(111)

    graph = FigureCanvasTkAgg(fig, master=top)
    graph.get_tk_widget().pack(fill='both')

    def plotter():
        while continuePlotting:
            ax.cla()
            dpts = data_points()
            y = dpts[0:-1]

            x = np.linspace(0,len(y),len(y))

            ax.plot(x, y)
            ax.grid(True)

            graph.draw()

            time.sleep(1)

    def gui_handler():
        change_state()
        threading.Thread(target=plotter).start()

    b = tk.Button(root, text="Start/Stop", command=gui_handler, bg="red", fg="white")
    b.pack()

    root.mainloop()

if __name__ == '__main__':
    app()

任何帮助都将不胜感激


Tags: importtruedatalentopdefnpline
1条回答
网友
1楼 · 发布于 2024-04-25 17:54:45

基本问题是,您是从非GUI线程调用Tk函数的。别那么做。Tk不是设计为从随机线程调用的。一般的解决方案被描述为这个站点上a question on tkinter thread communication的答案。简而言之,将计算出的数据推送到队列中,并引发Tk事件,让UI线程知道有更多的数据准备就绪。然后,事件处理程序可以从队列中获取新值并使用它执行UI操作。你知道吗

附件是使用此机制的脚本的修改版本。你知道吗

import tkinter as tk
import numpy as np

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import time
import threading
from queue import Queue

DATA_READY_EVENT = '<<DataReadyEvent>>'

continuePlotting = False
line = 0
data = np.random.rand(100, 500)

def change_state():
    global continuePlotting
    if continuePlotting == True:
        continuePlotting = False
    else:
        continuePlotting = True

def data_points():
    global line
    global data

    l = line % len(data) - 1
    r = data[l]

    line = line+1

    return r

def app():
    root = tk.Tk()
    root.configure(background='white')
    queue = Queue()
    # First Plot
    top = tk.Frame(root)
    top.pack(fill='both')

    fig = Figure()
    ax = fig.add_subplot(111)

    graph = FigureCanvasTkAgg(fig, master=top)
    graph.get_tk_widget().pack(fill='both')

    def plot(ev):
        x,y = queue.get()
        ax.plot(x, y)
        ax.grid(True)
        graph.draw()

    def plotter():
        global continuePlotting
        while continuePlotting:
            ax.cla()
            dpts = data_points()
            y = dpts[0:-1]
            x = np.linspace(0,len(y),len(y))
            queue.put((x,y))
            graph.get_tk_widget().event_generate(DATA_READY_EVENT)
            time.sleep(1)

    def gui_handler():
        change_state()
        threading.Thread(target=plotter).start()

    graph.get_tk_widget().bind(DATA_READY_EVENT, plot)
    b = tk.Button(root, text="Start/Stop", command=gui_handler, bg="red", fg="white")
    b.pack()

    root.mainloop()

if __name__ == '__main__':
    app()

相关问题 更多 >