0

I can't get a realtime value back to the GUI. I'm using Bleak and I want to return my device's RSSI in real time, and not after the scan end. I tryed to insert a thread, I have the same problem. Thanks for your help

# définition du thread
class MonThread (threading.Thread) :
    def __init__ (self, win) :
        threading.Thread.__init__ (self)
        self.win = win  
            
    async def callback(self,device: BLEDevice, advertisement_data: AdvertisementData):
      
      # Filtering by Macaddress
      if device.address=="AB:7E:48:E7:EE:1F":

          splitstr1=advertisement_data
          print('Name : ',splitstr1[0]) # NAME
          print('Signal : ',splitstr1[5]) # RSSI
          self.win.event_generate ("<<thread_fini>>", x = splitstr1[5])
                            

    async  def run (self): 
          
      scanner= BleakScanner(self.callback)
        #while True :
        
      await scanner.start()
      await asyncio.sleep(20)
      await scanner.stop() 

Here the GUI

def lance_thread () :
    m = MonThread (root)
    m.start ()

def thread_fini_fonction (e) :
    message2.config(text = str (e.x))
    


import tkinter as tk
root   = tk.Tk ()
root.title("Analyzer")
root.geometry('600x400+100+100')
root.resizable(True,True)
root.config(relief=tk.RAISED, bd=3) 

b = tk.Button(text="Scan", command=lance_thread)
b.pack()
frameH = tk.Frame(root,width=200, height=80, background="bisque",relief=tk.GROOVE, bd=6,padx=20,pady=20)
frameH.pack(anchor="nw",padx=10,pady=10,fill="x")

message = tk.Label(frameH, text="RSSI : ", height=2,padx=50,background="bisque")
message.grid(row=0, column=1)
message2 = tk.Label(frameH, text="---", height=2)
message2.grid(row=0, column=9)

root.bind ("<<thread_fini>>", thread_fini_fonction)


root.mainloop ()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    TKinter apps have an event loop and Asyncio has a different event loop. Running both is a problem. With a simple trick you can keep the UI responsive by calling Tkinters update method from an infinite loop in an asyncio awaitable function. e.g. https://stackoverflow.com/a/47896365/7721752 – ukBaz Feb 26 '23 at 11:03
  • Thanks a lot for this precious informations. I solved my problem – François HEBERT Feb 26 '23 at 20:45

0 Answers0