0

Excuse me for my language. I have written a python code that creates a GUI which gives a set of serial data and plot them in real time. I show the plots and data in different tabs, which are defined using notebook. The real time plot is also performed using the animation function of matplotlib library. But I have problem; After passing a time (a few minutes) when I switch between the tabs or minimize the window, the labels and images appear within about 10 seconds. They are shown with delay. Please HELP ME.Here is the code and the screenshots of the problem arises.1234

    from cProfile import label
    from tkinter.font import BOLD, ITALIC
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    import numpy as np
    from tkinter import *
    from PIL import Image, ImageTk
    import tkinter.ttk as ttk
    import matplotlib.pyplot as plt
    import os,sys


    import serial
    import matplotlib.animation as animation
    import time

    csfont = {'fontname': 'Comic Sans MS'}
    hfont = {'fontname': 'Times new roman'}

    
    # --------------------------------OPEN TWO IMAGES FOR NEXT USES ------------------------------------------------- #
    


    first_IMAGE = Image.open("first_image.jpg").resize((140, 140))
    second_IMAGE = Image.open("second_image.jpg").resize((140, 140))


    
    # -------------------------------------------- DEFINE ANIMATE FUNCTION------------------------------------------- #
    

    def animate(i, dataListP1, dataListP2, dataListP3, dataListP4, dataListP5, dataListP6, dataListP7, dataListP8, dataListP9, ser):

        

        ser.write(b'g')
        arduinoData_string = ser.readline().decode("utf-8").strip('\r\n')
        try:

            arduinoData_float = [float(x) for x in arduinoData_string.split()]

            dataListP1.append(arduinoData_float[1])
            VP1 = arduinoData_float[1]

            dataListP2.append(arduinoData_float[2])
            VP2 = arduinoData_float[2]

            dataListP3.append(arduinoData_float[3])
            VP3 = arduinoData_float[3]

            dataListP4.append(arduinoData_float[4])
            VP4 = arduinoData_float[4]

            dataListP5.append(arduinoData_float[5])
            VP5 = arduinoData_float[5]

            dataListP6.append(arduinoData_float[6])
            VP6 = arduinoData_float[6]

            dataListP7.append(arduinoData_float[7])
            VP7 = arduinoData_float[7]

            dataListP8.append(arduinoData_float[8])
            VP8 = arduinoData_float[8]

            dataListP9.append(arduinoData_float[9])
            VP9 = arduinoData_float[9]

        except:
            pass

        labelangle_1V.config(text=f'{int(VP7)}')
        labelangle_2V.config(text=f'{int(VP8)}')
        labelangle_3V.config(text=f'{int(VP9)}')
    

        if VP4 >= 0 and VP4 < 10:
            colorL = "green"

        elif VP4 >= 10 and VP4 < 20:
            colorL = "yellow"

        elif VP4 >= 20 and VP4 < 30:
            colorL = "orange"

        elif VP4 >= 30 and VP4 <= 40:
            colorL = "red"
        
        CT1.create_rectangle(10, 20, 210, 70, width=2,outline='#6C6A61', fill="#B1D4E0")
        CT1.create_rectangle(10, 20, VP4*5+10, 70, width=2,outline=colorL, fill=colorL)

        CT1.create_rectangle(10, 20, 210, 70, width=4, outline="#6C6A61")
        CT1.create_line(60, 20, 60, 70, width=4, fill="#6C6A61")
        CT1.create_line(110, 20, 110, 70, width=4, fill="#6C6A61")
        CT1.create_line(160, 20, 160, 70, width=4, fill="#6C6A61")
        labelTemp1V.config(text=f'{int(VP4)}')

    


    

        if VP5 >= 0 and VP5 < 10:
            colorC = "green"
            

        elif VP5 >= 10 and VP5 < 20:
            colorC = "yellow"
            


        elif VP5 >= 20 and VP5 < 30:
            colorC = "orange"

        elif VP5 >= 30 and VP5 <= 40:
            colorC = "red"

        CT2.create_rectangle(10, 20, 210, 70, width=2,outline='#6C6A61', fill="#B1D4E0")
        CT2.create_rectangle(10, 20, VP5*5+10, 70, width=2,outline=colorC, fill=colorC)
        CT2.create_rectangle(10, 20, 210, 70, width=4, outline="#6C6A61")
        
        CT2.create_line(60, 20, 60, 70, width=4, fill="#6C6A61")
        CT2.create_line(110, 20, 110, 70, width=4, fill="#6C6A61")
        CT2.create_line(160, 20, 160, 70, width=4, fill="#6C6A61")
        labelTemp2V.config(text=f'{int(VP5)}')

    
    #
    

        if VP6 >= 0 and VP6 < 10:
            colorR = "green"

        elif VP6 >= 10 and VP6 < 20:
            colorR = "yellow"

        elif VP6 >= 20 and VP6 < 30:
            colorR = "orange"

        elif VP6 >= 30 and VP6 <= 40:
            colorR = "red"
        
        CT3.create_rectangle(10, 20, 210, 70, width=2,outline='#6C6A61', fill="#B1D4E0")
        CT3.create_rectangle(10, 20, VP6*5+10, 70, width=2,outline=colorR, fill=colorR)
        CT3.create_rectangle(10, 20, 210, 70, width=4, outline="#6C6A61")
        
        CT3.create_line(60, 20, 60, 70, width=4, fill="#6C6A61")
        CT3.create_line(110, 20, 110, 70, width=4, fill="#6C6A61")
        CT3.create_line(160, 20, 160, 70, width=4, fill="#6C6A61")
        labelTemp3V.config(text=f'{int(VP6)}')


    
    #
    

        dataList1P1 = dataListP1[-50:]
        dataList1P2 = dataListP2[-50:]
        dataList1P3 = dataListP3[-50:]
        
    

        ax1.clear()
        ax1.plot(dataList1P1, 'b')
        ax1.grid()

        ax1.set_ylim([-1,1])
        ax1.set_title("plot 1", **hfont, fontsize=18)
        ax1.tick_params(axis='both', which='major', labelsize=13)

        #  ---------------------------------------------
        ax2.clear()
        ax2.plot(dataList1P2, 'r')
        ax2.grid()

        ax2.set_ylim([-1,1])
        ax2.set_title("plot 2", **hfont, fontsize=18)
        ax2.tick_params(axis='both', which='major', labelsize=13)
        #  ---------------------------------------------
        ax3.clear()
        ax3.plot(dataList1P3, '#FFC300')
        ax3.grid()

        ax3.set_ylim([0,1.5])
        ax3.set_title("plot 3", **hfont, fontsize=18)
        ax3.tick_params(axis='both', which='major', labelsize=13)

        #  ---------------------------------------------
        #  ---------------------------------------------
        for tick in ax1.get_xticklabels():
            tick.set_fontname("Times new roman")

        for tick in ax1.get_yticklabels():
            tick.set_fontname("Times new roman")
        #  ---------------------------------------------
        #  ---------------------------------------------
        for tick in ax2.get_xticklabels():
            tick.set_fontname("Times new roman")

        for tick in ax2.get_yticklabels():
            tick.set_fontname("Times new roman")
        #  ---------------------------------------------
        #  ---------------------------------------------
        for tick in ax3.get_xticklabels():
            tick.set_fontname("Times new roman")

        for tick in ax3.get_yticklabels():
            tick.set_fontname("Times new roman")
        #  ---------------------------------------------


    
    dataList11 = []
    dataList22 = []
    dataList33 = []
    dataList44 = []
    dataList55 = []
    dataList66 = []
    dataList77 = []
    dataList88 = []
    dataList99 = []

    root = Tk()
    root.title('GUI test code')
    root.iconbitmap('tree1.ico')
    root.configure(background='#B1D4E0')
    w = 1460
    h = 930
    root.geometry('%dx%d+%d+%d' % (w, h, 0, 0))
    # root.state('zoomed')
    root.resizable(width=False, height=False)

    mainframe = ttk.Notebook(root)
    mainframe.pack(fill=BOTH, expand=True)

    fig = Figure(constrained_layout=True)

    ax1 = fig.add_subplot(131)
    ax2 = fig.add_subplot(132)
    ax3 = fig.add_subplot(133)

    
    # ----------------------------------------------- DEFINE STYLE -------------------------------------------------- #
    

    style = ttk.Style()

    style.theme_create('pastel', settings={
        ".": {
            "configure": {
                "background": '#B1D4E0',  # All except tabs
                "font": 'red'
            }
        },
        "TNotebook": {
            "configure": {
                "background": '#848a98',  # Your margin color
                "tabmargins": [2, 5, 0, 0],  # margins: left, top, right, separator
            }
        },
        "TNotebook.Tab": {
            "configure": {
                "background": '#d9ffcc',  # tab color when not selected
                # [space between text and horizontal tab-button border, space between text and vertical tab_button border]
                "padding": [20, 2],
                "font": "Times 15 bold"
            },
            "map": {
                "background": [("selected", '#ccffff')],  # Tab color when selected
                "expand": [("selected", [1, 1, 1, 0])]  # text margins
            }
        }
    })

    style.theme_use('pastel')

    
    # ---------------------------------------------CREATE THREE TABS------------------------------------------------- #
    

    tab1 = LabelFrame(mainframe, bg="#B1D4E0")
    mainframe.add(tab1, text=f'{"TAB 1" : ^30s}')

    

    tab2 = ttk.Frame(mainframe)
    mainframe.add(tab2, text=f'{"TAB 2" : ^30s}')

    

    tab3 = ttk.Frame(mainframe)
    mainframe.add(tab3, text=f'{"TAB 3" : ^30s}')

    
    
    

    subframeL1U = LabelFrame(tab1, bg="#B1D4E0")
    subframeL1U.place(relx=0, rely=0, relwidth=1, relheight=0.18)

    

    subframeL1M = LabelFrame(tab1, bg="#B1D4E0")
    subframeL1M.place(relx=0, rely=0.18, relwidth=1, relheight=0.6)

    

    # ------------------the lower frame- three text labels, three temp(var) labels, three bars ---------------------- #

    subframeL1L1 = Frame(tab1, bg="#B1D4E0")
    subframeL1L1.place(relx=0, rely=0.78, relwidth=1/3, relheight=0.22)

    
    

    subframeL1L2 = Frame(tab1, bg="#B1D4E0")
    subframeL1L2.place(relx=1/3, rely=0.78, relwidth=1/3, relheight=0.22)

    

    subframeL1L3 = Frame(tab1, bg="#B1D4E0")
    subframeL1L3.place(relx=2/3, rely=0.78, relwidth=1/3, relheight=0.22)

    
    # ------------------------------- TWO IMAGES and LABLE ON UPPER FRAME ------------------------------------------- #
    

    LU1 = Label(subframeL1U, text="Testing the GUI", font=[
        'B Titr', 30], bg='#B1D4E0', bd=4)

    LU1.place(relx=0.25, rely=0.2, relwidth=0.5, relheight=0.6)

    first_image1 = ImageTk.PhotoImage(first_IMAGE, Image.Resampling.LANCZOS)
    first_image1_label = Label(
        subframeL1U, image=first_image1, bg='#B1D4E0')
    first_image1_label.place(relx=0, rely=0, relwidth=0.1, relheight=1)
    first_image1_label.image = first_image1

    second_image = ImageTk.PhotoImage(
        second_IMAGE, Image.Resampling.LANCZOS)
    second_image_label = Label(
        subframeL1U, image=second_image, bg='#B1D4E0')
    second_image_label.place(relx=0.9, rely=0, relwidth=0.1, relheight=1)
    second_image_label.image = second_image

    
    # ------------------------------------ THREE ANGLE LABELS IN TAB 2 ---------------------------------------------- #
    

    labelangle_1 = Label(tab2, text="Angle 1 : ", bg="#B1D4E0", font=(
        'Times new roman', 20, ITALIC, BOLD))
    labelangle_1.place(relx=0.3, relwidth=0.2, rely=0.1, relheight=0.2)

    
    labelangle_1V = Label(
        tab2, text="", bg="#B1D4E0", fg="blue", font=('Times new roman', 30, BOLD))
    labelangle_1V.place(relx=0.5, relwidth=0.05, rely=0.1, relheight=0.2)

    

    labelangle_1U = Label(
        tab2, text="\u00b0", bg="#B1D4E0", font=('Times new roman', 20, BOLD))
    labelangle_1U.place(relx=0.55, rely=0.1, relheight=0.2)

    
    

    labelangle_2 = Label(tab2, text="Angle 2 : ", bg="#B1D4E0", font=(
        'Times new roman', 20, ITALIC, BOLD))
    labelangle_2.place(relx=0.3, relwidth=0.2, rely=0.4, relheight=0.2)

    

    labelangle_2V = Label(
        tab2, text="", bg="#B1D4E0", fg='red', font=('Times new roman', 30, BOLD))
    labelangle_2V.place(relx=0.5, relwidth=0.05, rely=0.4, relheight=0.2)

    

    labelangle_2U = Label(
        tab2, text="\u00b0", bg="#B1D4E0", font=('Times new roman', 20, BOLD))
    labelangle_2U.place(relx=0.55, rely=0.4, relheight=0.2)

    
    

    labelangle_3 = Label(tab2, text="Angle 3 : ", bg="#B1D4E0", font=(
        'Times new roman', 20, ITALIC, BOLD))
    labelangle_3.place(relx=0.3, relwidth=0.2, rely=0.7, relheight=0.2)

    

    labelangle_3V = Label(tab2, text="", bg="#B1D4E0", font=(
        'Times new roman', 30, BOLD), fg='#FFC300')
    labelangle_3V.place(relx=0.5, relwidth=0.05, rely=0.7, relheight=0.2)

    

    labelangle_3U = Label(
        tab2, text="\u00b0", bg="#B1D4E0", font=('Times new roman', 20, BOLD))
    labelangle_3U.place(relx=0.55, rely=0.7, relheight=0.2)


    
    # --------------------------- THREE LABELS IN EACH OF THREE LOWER SUBFRAMES ------------------------------------- #
    

    labelTemp1 = Label(subframeL1L1, text=" temperature 1 : ", bg="#B1D4E0", font=(
        'Times new roman', 20, ITALIC, BOLD))
    labelTemp1.place(relx=0.1, relwidth=0.5, rely=0.05, relheight=0.3)

    
    labelTemp1V = Label(
        subframeL1L1, text="", bg="#B1D4E0", fg="blue", font=('Times new roman', 30, BOLD))
    labelTemp1V.place(relx=0.6, relwidth=0.15, rely=0.05, relheight=0.3)

    

    labelTemp1U = Label(
        subframeL1L1, text="\u00b0C", bg="#B1D4E0", font=('Times new roman', 20, BOLD))
    labelTemp1U.place(relx=0.75, rely=0.05, relheight=0.3)

    
    

    labelTemp2 = Label(subframeL1L2, text="temperature 2 : ", bg="#B1D4E0", font=(
        'Times new roman', 20, ITALIC, BOLD))
    labelTemp2.place(relx=0.2, relwidth=0.5, rely=0.05, relheight=0.3)

    

    labelTemp2V = Label(
        subframeL1L2, text="", bg="#B1D4E0", fg='red', font=('Times new roman', 30, BOLD))
    labelTemp2V.place(relx=0.7, relwidth=0.15, rely=0.05, relheight=0.3)

    

    labelTemp2U = Label(
        subframeL1L2, text="\u00b0C", bg="#B1D4E0", font=('Times new roman', 20, BOLD))
    labelTemp2U.place(relx=0.85, rely=0.05, relheight=0.3)

    
    

    labelTemp3 = Label(subframeL1L3, text="temperature 3 : ", bg="#B1D4E0", font=(
        'Times new roman', 20, ITALIC, BOLD))
    labelTemp3.place(relx=0.15, relwidth=0.5, rely=0.05, relheight=0.3)

    

    labelTemp3V = Label(subframeL1L3, text="", bg="#B1D4E0", font=(
        'Times new roman', 30, BOLD), fg='#FFC300')
    labelTemp3V.place(relx=0.65, relwidth=0.15, rely=0.05, relheight=0.3)

    

    labelTemp3U = Label(
        subframeL1L3, text="\u00b0C", bg="#B1D4E0", font=('Times new roman', 20, BOLD))
    labelTemp3U.place(relx=0.8, rely=0.05, relheight=0.3)



    
    # ------------------------------------THREE CANVAS FOR SHOW TEMP BAR--------------------------------------------- #
    

    CT1 = Canvas(subframeL1L1, bg='#B1D4E0', highlightthickness=0)
    CT1.place(relx=0.25, rely=0.35, relwidth=0.6, relheight=0.65)


    lT1 = Label(CT1, text="0\u00b0C", font=("times new roman", 16, BOLD),
                bg='#B1D4E0', fg='#444444').place(relx=0, rely=0.67)
    lT2 = Label(CT1, text="40\u00b0C", font=("times new roman", 16, BOLD),
                bg='#B1D4E0', fg='#444444').place(relx=0.65, rely=0.67)

    

    CT2 = Canvas(subframeL1L2, bg='#B1D4E0', highlightthickness=0)
    CT2.place(relx=0.25, rely=0.35, relwidth=0.6, relheight=0.65)


    lT3 = Label(CT2, text="0\u00b0C", font=("times new roman", 16, BOLD),
                bg='#B1D4E0', fg='#444444').place(relx=0, rely=0.67)
    lT4 = Label(CT2, text="40\u00b0C", font=("times new roman", 16, BOLD),
                bg='#B1D4E0', fg='#444444').place(relx=0.65, rely=0.67)

    

    CT3 = Canvas(subframeL1L3, bg='#B1D4E0', highlightthickness=0)
    CT3.place(relx=0.25, rely=0.35, relwidth=0.6, relheight=0.65)


    lT5 = Label(CT3, text="0\u00b0C", font=("times new roman", 16, BOLD),
                bg='#B1D4E0', fg='#444444').place(relx=0, rely=0.67)
    lT6 = Label(CT3, text="40\u00b0C", font=("times new roman", 16, BOLD),
                bg='#B1D4E0', fg='#444444').place(relx=0.65, rely=0.67)

    
    # ----------------------------------- CREATE CONVAS TO SHOW THE FIGURE ------------------------------------------ #
    

    canvas = FigureCanvasTkAgg(fig, master=subframeL1M)
    canvas.get_tk_widget().place(relx=0, rely=0, relwidth=1, relheight=1)

    
    
    ser = serial.Serial("COM3", 19200)
    time.sleep(2)

    ani = animation.FuncAnimation(fig, animate, frames=100, fargs=(dataList11, dataList22, dataList33, dataList44,dataList55,dataList66,dataList77,dataList88,dataList99, ser), interval=100)

    root.state('zoomed')
    mainframe.mainloop()
    root.mainloop()

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Try to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – HoRn Feb 20 '23 at 00:59

0 Answers0