Tkinter GUI matplotlib 没有响应

问题描述 投票:0回答:0

在给定的代码中,我试图在 Tkinter GUI 上使用 matplotlib 绘制实时串行数据。代码运行良好,但在我按下任一按钮(开始或停止)后 GUI 变得无响应。

data1 = np.array([])
cond = False


def plot_data():
    global cond, data1

    msg = np.array([])
    concat_msg = np.array([])
    counter = 0

    l1 = np.array([])

    try:
        if cond == True:
            s.reset_input_buffer()
            while counter < msg_len:  #ud
                a = s.read().hex()
                msg = np.append(msg, a)
                counter += 1
            
            print(msg)

            if(msg[0]=='07' and  msg[1]=='e0'):
                
                print("Condition is True")

                # Filter
                msg = filter(lambda x: x != '07' and x != 'e0' and x != 'ff' and x != 'a3' and x != 'a7' and x!= 'c6' and x != 'd4', msg)
                msg = list(msg)
                
                concat_msg = [''.join(x) for x in zip(msg[0::2], msg[1::2])]

                for i in range(len(concat_msg)):
                    concat_msg[i] = int(concat_msg[i], 16)

                len_each = int(len(concat_msg))

                l1 = concat_msg[0:len_each]

                if len(data1) < len_each: #ud
                    data1 = np.append(data1, l1[0:len_each]) #ud

                lines.set_xdata(np.arange(0,len(data1)))
                lines.set_ydata(data1)

                canvas.draw()

                data1 = np.array([])

                concat_msg = np.array([])

            else:
                print("Condition is False")
                s.reset_input_buffer()
                s.close()

        root.after(1,plot_data)

    except KeyboardInterrupt:
        root.destroy()

#################################################################
###################### Button Functions #########################
#################################################################
def plot_start():
    global cond
    # s.reset_input_buffer()
    s.write(b"PORT IS OPEN")
    cond = True
    s.reset_input_buffer()

def plot_stop():
    global cond
    cond = False

def hide_window():
   root.after(1, screenshot)

root = tk.Tk()
root.title("Real time wala plot")
root.configure(background= 'light blue')
root.geometry("1920x1080")

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

ax.set_title("FFT UP")
ax.set_xlabel("Sample")
ax.set_ylabel("values")
ax.set_xlim(0,(((msg_len-5)/2))) #ud
ax.set_ylim(0,70000)
ax.grid()
lines = ax.plot([],[])[0]

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().place(x=0, y=0, width=1920, height=1080)
canvas.draw()

#Configure Rows and column

tk.Grid.rowconfigure(root, 2,weight=1)
tk.Grid.columnconfigure(root,1,weight=1)

myFont = font.Font(size=12, weight='bold')

root.update()
start = tk.Button(root, text="S\nT\nA\nR\nT", height= 1, width=5, fg='white' , bg='Green', command=lambda: plot_start())
start['font'] = myFont

root.update()
stop = tk.Button(root, text="S\nT\nO\nP", height= 1, width=5, fg='yellow', bg='RED', command=lambda: plot_stop())
stop['font'] = myFont

buttons= [start, stop]

row_no=0
#Loop through all the buttons and configure it row-wise
for button in buttons:
   tk.Grid.rowconfigure(root,row_no, weight=1)
   row_no+=1

#Adjust the position in grid and make them sticky

start.grid(row=0, column=0, sticky= "nsew")
stop.grid(row=1, column=0, stick= "nsew")

#################################################################
###################### Set serial port ##########################
#################################################################

s = sr.Serial('COM15', 115200) #ud
s.reset_input_buffer()

print(s)

t1 = threading.Thread(target=plot_data)
t1.start()
root.mainloop()

我已尝试使用多处理和线程来解决此问题,但没有任何效果。请帮助!!!

编辑: 我正在使用 MOXA UPort 1250 以 115200 的波特率使用 RS-422 协议接收串行数据。我在 mainloop() 之前删除了多余的 after() 方法并放置了 plot_data() 在线程上。现在,GUI 的响应速度有所提高,但在代码执行过程中,无论何时拖动或移动窗口,它仍然会滞后。这是正常的还是有什么问题?

matplotlib tkinter multiprocessing python-multiprocessing python-multithreading
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.