0

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()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
kvar rnd
  • 17
  • 1
  • Whose code is "your code"? – martineau Sep 22 '19 at 06:57
  • Yes my code. but i've taken a patch from your code and try to update it. – kvar rnd Sep 22 '19 at 07:22
  • Sorry, I have no idea what code of mine you're talking about. – martineau Sep 22 '19 at 07:25
  • The code for changing of an image periodically. here's the link below: https://stackoverflow.com/questions/38552086/how-can-i-periodically-change-a-tkinter-image. – kvar rnd Sep 22 '19 at 07:28
  • i just need to update the images which are getting change with the value im trying to insert periodically – kvar rnd Sep 22 '19 at 07:29
  • 1
    `import tkinter as Tk` `import tkinter as tk` `from tkinter import *` You got that covered! – Reblochon Masque Sep 22 '19 at 07:35
  • Sorry I don't understand your question. – martineau Sep 22 '19 at 07:45
  • how i can print values on a particular image and when the image is changed the values should also change periodically. – kvar rnd Sep 22 '19 at 08:45
  • kvar rnd: First of all I am just one of many others answering questions here, so most folks reading your question will have no idea who the "you" is and what the code you're talking is. Second of all, you need to post a [mre] that doesn't have any unrelated code and dependencies in it (such as `websocket`) so others can easily run the code themselves. – martineau Sep 23 '19 at 23:50

0 Answers0