I am showing encoder value on tkinter label at the same time I have a function which is updated in every 100ms. So my problem is my encoder value is not updated in real time. How to solve this issue ? My code is here
if __name__ == "__main__":
import os
import board
import busio
import pigpio
import serial
import tkinter as tk
import rotary_encoder
import Adafruit_MCP4725
import RPi.GPIO as GPIO
from PIL import ImageTk,Image
s=serial.Serial(port='/dev/ttyS0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0.05)
dac=Adafruit_MCP4725.MCP4725(address=0x61)
pi = pigpio.pi()
path="/home/pi/logo.png"
root=tk.Tk()
img=ImageTk.PhotoImage(Image.open(path))
panel=tk.Label(root,image=img)
panel.pack()
panel.place(x=930,y=1,height=50,width=70)
root.title("Dynalec")
root.iconphoto(False,img)
root.geometry("1000x600")
Dsel = 0
cal_c= 0
pos=0
rpm = 0
torq=0
arr = [0,0,0,0,0]
def callback(way): # interrupt event sense on pin no 17,18
global pos
pos += way
if pos >= 9999:
pos=9999
if pos <= 0:
pos=0
var.set(pos)
dac.set_voltage(int(pos/2.441))
def rpm_update():
global rpm
s.write(str.encode(i))
time.sleep(0.1)
if s.inWaiting():
rpm = s.readline(s.inWaiting())
rpm = rpm.decode('utf-8',errors='ignore')
rpm = rpm[5:-1]
var2.set(rpm)
def update():
global rpm,torq
try:
rpm_update()
var3.set(torq)
pow = (float(torq)*float(avg_rpm))/9549
pow=format(pow,'.2f')
var4.set(pow)
except ZeroDivisionError:
pass
root.after(100,update)
#RPM Label
L1=tk.Label(root,text="RPM/ %TC posn",fg="blue",font=("TimesNewroman",30,'bold'))
L1.pack()
L1.place(x=65,y=380)
#RPM DEMAND value
var=tk.StringVar()
L2=tk.Label(root,fg="red",textvariable=var,font=("TimesNewroman",35))
L2.pack()
L2.place(x=65,y=431,height=50,width=280)
#PRESENT RPM Label
L5=tk.Label(root,text="RPM",fg="red",font=("TimesNewroman",45,'bold'))
L5.pack()
L5.place(x=220,y=55,height=70,width=240)
#PRESENT RPM value
var2=tk.StringVar()
L6=tk.Label(root,fg="black",textvariable=var2,font=("TimesNewroman",70,'bold'))
L6.pack()
L6.place(x=220,y=116,height=90,width=240)
"""
torque and power label for `var3` and `var4`.
"""
decoder=rotary_encoder.decoder(pi, 17, 18, callback)
root.update()
root.mainloop()
I think due to root.update() this issue arises. But I need root.update() function also for this project. So I need to update var.set() value when ever data will be there but var2.set() in every 100ms. Do I need multithread?? So how can I ressolve this .