'Running infinite While loop with Ethernet communication and showing response on the GUI

We are trying to use an ethernet communication model on the raspberry Pi that receives data from an Arduino in an infinite while loop. The response of the communication is then displayed onto the GUI which is running in parallel. The flags are being received by the Pi but the gui does not display the required images.

from tkinter import *
from PIL import ImageTk,Image
from socket import *
import select
import numpy as np
from threading import Thread
from time import sleep
global data

data = None 
timeout = 3 # timeout in seconds
msg = "test"
host = "192.168.1.101"
print ("Connecting to " + host)
port = 5000
s = socket(AF_INET, SOCK_STREAM)
print ("Socket made")
ready = select.select([s],[],[],timeout)
s.connect((host,port))
print("Connection made")

def func2():
    while True:
        data = s.recv(4096)
        print("Data received")
        print (data)
        
if __name__ == '__main__':
    Thread(target = func2).start()

w=Tk() 
w.geometry('1700x900')
w.configure(bg='#ffffff')
w.resizable(True,True)
w.title('SensorNode User Interface')
imagegreen = ImageTk.PhotoImage(Image.open("green.png"))
imagered = ImageTk.PhotoImage(Image.open("red.png"))

def func1():
    while True:
        if (data == 0):
            Button(w,
                   image=imagered,
                   border=0,
                   bg='#ffffff',
                   activebackground='#ffffff').place(x=630,y=460)
            sleep(0.1)
        if (data == 1):
            Button(w,
                   image=imagegreen,
                   border=0,
                   bg='#ffffff',
                   activebackground='#ffffff').place(x=630,y=460)
            sleep(0.1)
        
if __name__ == '__main__':
    Thread(target = func1).start()

w.mainloop()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source