在tkinter框架中更新matplotlib plot

b5buobof  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(316)

我试图在tkinter gui中绘制串行端口的数据。只有当特定的数据包来自串行数据包时,才应绘制和更新该图。我可以解析新的传入数据并更新gui(文本区域)。但是当我从“update\u gui”线程调用“plot()”函数时,程序退出,我得到
“进程已完成,退出代码为-1073741819(0xc0000005)”
信息。
相反,如果我从其他地方调用“plot()”(command button,或者在mainloop()之前),则生成并显示绘图。
以下是代码的相关部分:

import threading
import tkinter as tk
import tkinter.scrolledtext as st
import rx_seriale as rx_ser
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
import queue

running = True
serial_data = ''
filter_data = ''
update_period = 5
serial_object = None
gui = tk.Tk()
gui.title("UART Interface")
my_queue = queue.Queue()
t1 = ''

def plot(valori):
    global frame_plot1

    try:
        # the figure that will contain the plot
        fig = Figure(figsize=(4.5, 4), dpi=100)

        # adding the subplot
        plot1 = fig.add_subplot(111)

        #dummy values
        y = [i**2 for i in range(101)]

        # plotting the graph
        plot1.plot(y)

        # creating the Tkinter canvas
        # containing the Matplotlib figure
        canvas = FigureCanvasTkAgg(fig, master=frame_plot1)
        canvas.draw()

        # placing the canvas on the Tkinter window
        canvas.get_tk_widget().pack()

        # creating the Matplotlib toolbar
        #toolbar = NavigationToolbar2Tk(canvas, frame_plot1)
        #toolbar.update()

        # placing the toolbar on the Tkinter window
        #canvas.get_tk_widget().pack()
    except Exception as e:
        print('Errore:' + str(e))

def update_gui():
    global filter_data
    global update_period
    global my_queue
    global type_test, test_status,flag

    while (1):

        data = my_queue.get(block=True)

        text.insert('end', test_status[data[0]] + " - " + type_test[data[1]])
        text.insert('end', '\n')
        text.see('end')

        if (data[1] == 6):
            plot(1)

if __name__ == "__main__":

'''
...
all the stuff for design TK windows
...
'''

    # threads
    t2 = threading.Thread(target=update_gui)
    t2.daemon = True
    t2.start()

    # mainloop
    gui.geometry('1000x500')
    gui.mainloop()

出什么问题了?谢谢您。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题