Home > Net >  how can I stop the flickering and overlapping in the code below tkinter?
how can I stop the flickering and overlapping in the code below tkinter?

Time:07-13

def st():

    if len(ser.readline())>0:

        pass

        data_recv=ser.readline().decode('utf-8').strip()

        datas=data_recv.split(",")

        if (len(datas)>3):
         
            c_x = ttk.Label(root,text=datas[1]).place(x=480,y=185)
            
            c_y=ttk.Label(root,text=datas[2]).place(x=480,y=210)

            c_z=ttk.Label(root,text=datas[3]).place(x=480,y=235)

    root.after(100, st)

tkinter labels are flickering and overlap when reading from the serial port. how can I stop the flickering and overlapping when displaying on Tkinter GUI.

CodePudding user response:

Something like that should work, I couldn't test it fully because I don't have a serial device here. You have to adjust the serial port but the concept should be clear. As mentioned in the comments above I took the labels out of the function and now it updates just the text of the labels by running the function st().

# imports
import tkinter as tk
from tkinter import ttk
import serial

# config serial port
ser = serial.Serial('COM8')
ser.baudrate = 115200

def st():
    if len(ser.readline())>0:
        data_recv=ser.readline().decode('utf-8').strip()
        datas=data_recv.split(",")

        if (len(datas)>3):
            c_x.config(text=datas[1])
            c_y.config(text=datas[2])
            c_z.config(text=datas[3])
    root.after(100, st)

root = tk.Tk()
root.geometry('600x500')
c_x = ttk.Label(root,text='')
c_x.place(x=280,y=185)   
c_y = ttk.Label(root,text='')
c_y.place(x=280,y=210)
c_z = ttk.Label(root,text='')
c_z.place(x=280,y=235)
st()

root.mainloop()

CodePudding user response:

    #imports
    import tkinter as tk
    from tkinter import ttk
    import serial
    #config serial port
    ser = serial.Serial('COM8')
    ser.baudrate = 115200
    def st():
        if len(ser.readline())>0:
            data_recv=ser.readline().decode('utf-8').strip()
            datas=data_recv.split(",")
            if (len(datas)>3):
                c_x.config(text=datas[1])
                c_y.config(text=datas[2])
                c_z.config(text=datas[3])
        root.after(100, st)
   root = tk.Tk()
   root.geometry('600x500')
   c_x = ttk.Label(root,text=datas[1])
   c_x.place(x=280,y=185)   
   c_y = ttk.Label(root,text=datas[2])
   c_y.place(x=280,y=210)
   c_z = ttk.Label(root,text=datas[3])
   c_z.place(x=280,y=235)
   st()
   root.mainloop()

This worked for me.

  • Related