I've 2 questions on your code of changing an image periodically. I've run your code and the images are changing perfectly fine, but when im trying to include your patch with mine the images isn't in order and random images are generating. my code is about web-socket program where the rpi is the client and esp32 is the server and the data is coming in sprintf format.
The thing is that I've to take that data and paste on the images I've in my dir. there are 4 images and each image has 4 different parameters.
My 2nd question is that when the image is change the data should also get change periodically. also can i set the timings of an image like which image should get display first and its data too.
It'd be a great help if you can enlighten me on this. i'm putting my code below for your reference.
CODE :
import tkinter as Tk
import tkinter as tk
from tkinter import *
import websocket
from PIL import Image, ImageTk
from parse import *
import image
import itertools
import os
import sys
import shutil
import threading
import time
def update_image(dst):
test_image = '/home/pi/Desktop/kvar pix exb/KVAR PIX P1.jpg', '/home/pi/Desktop/kvar pix exb/KVAR PIX P2.jpg', '/home/pi/Desktop/kvar pix exb/KVAR PIX P3.jpg','/home/pi/Desktop/kvar pix exb/KVAR PIX P4.jpg'
for src in itertools.cycle(test_image):
shutil.copy(src, dst)
time.sleep(1) # pause between updates
def refresh_image(canvas,img,image_path,image_id):
try:
pil_img = Image.open(image_path).resize((width_value,height_value), Image.ANTIALIAS)
img = ImageTk.PhotoImage(pil_img)
canvas.itemconfigure(image_id,image=img)
except IOError:
img = None
#repeat every half sec
canvas.after(500,refresh_image,canvas,img,image_path,image_id)
window = tk.Tk()
image_path = 'test.png'
#comparing the incoming data and addr given by the server
comparing_data = ['\x02','45']
#getting values from server
labeltxt_1 = StringVar()
labeltxt_2 = StringVar()
labeltxt_3 = StringVar()
labeltxt_4 = StringVar()
labeltxt_5 = StringVar()
labeltxt_6 = StringVar()
labeltxt_7 = StringVar()
def comm_loop():
global result,labeltxt_1
ws = websocket.WebSocket()
ws.connect("ws://192.168.4.1:7/")
while 1:
result = ws.recv()
incoming_data = result.split(',')
while 1:
if (incoming_data[1] != comparing_data[1]):
print("unauthorised server")
else:
break
print(incoming_data)
labeltxt_1.set(' '+incoming_data[2])
labeltxt_2.set(' '+incoming_data[3])
labeltxt_3.set(' '+incoming_data[4])
labeltxt_4.set(' '+incoming_data[5])
labeltxt_5.set(' '+incoming_data[6])
labeltxt_6.set(' '+incoming_data[7])
labeltxt_7.set(' '+incoming_data[8])
ws.close()
#threading
thread = threading.Thread(target=comm_loop)
thread.daemon = True
thread.start()
#threading
th = threading.Thread(target = update_image, args=(image_path,))
th.daemon = True
th.start()
while not os.path.exists(image_path):
time.sleep(0.1)
width_value = window.winfo_screenwidth()
height_value = window.winfo_screenheight()
window.attributes("-fullscreen", True)
window.config(highlightthickness = 0)
canvas = Canvas(window,width=1920,height=1080)
label_1 = Label(window, textvariable = labeltxt_1, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 600, y = 355)
label_2 = Label(window, textvariable = labeltxt_2, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 640, y = 530)
label_3 = Label(window, textvariable = labeltxt_3, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 850, y = 710)
label_4 = Label(window, textvariable = labeltxt_4, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 730, y = 880)
for i in range(0,0.5):
i=i+1
label_5 = Label(window, textvariable = labeltxt_5, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 600, y = 355)
label_6 = Label(window, textvariable = labeltxt_6, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 640, y = 530)
label_7 = Label(window, textvariable = labeltxt_7, fg = "#FF8000", font = ("Times",78), bg = "#FFFFFF").place(x = 850, y = 710)
img = None
image_id = canvas.create_image(0,0,anchor=NW,image=img)
canvas.pack()
refresh_image(canvas,img,image_path,image_id)
window.mainloop()