Python Forum
add svg to 2 thread program and display tabel in GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add svg to 2 thread program and display tabel in GUI
#4
(May-03-2023, 03:55 PM)deanhystad Wrote: So no more tkinter? If you want to write a spreadsheet I would use pandas. First verify that you are getting values.

Hi! First i want to safe my data in a file, then i want to dispay them in tkinter, so step by step. :)

Thanks for the tipp with pandas, i have installed them and implement them into my code:

import pandas as pd

import asyncio
import threading
from tkinter import *
from bleak import BleakClient
import numpy
from prettytable import PrettyTable
import openpyxl
from datetime import datetime


address = "CC:50:E3:9C:15:02"
MODEL_NBR_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8"

table = PrettyTable()
table.field_names = ["Wert 1", "Wert 2"]

wb = openpyxl.Workbook() # Workbook-Objekt erzeugen
ws = wb.active #Arbeitsblatt auswählen

now = datetime.now()
dt_string = now.strftime("%d-%m-%Y_%H-%M-%S") # Variable mit Datum und Uhrzeit erzeugen

filename = f"{dt_string}.xlsx" #Datei mit Name und Datum erzeugen

ws["A1"] = "Red"
ws["B1"] = "IR" #Überschriften hinzufügen

last_red_row = 1  # Die erste Zeile ist bereits mit Überschriften belegt
last_ir_row = 1

spo2_list = []
ir_list = []






def print_table():
    print(table)

class BLEThread(threading.Thread):
    def __init__(self, address):
        threading.Thread.__init__(self)
        self.address = address


    async def handle_uuid1_notify(self, sender, data):
        spo2_values = numpy.frombuffer(data, dtype=numpy.uint32)
        print("Red: {0}".format(spo2_values))
        for spo2_value in spo2_values:
            table.add_row([spo2_value, ""])
            ws.append([spo2_value, ""])  # Zeile hinzufügen
            spo2_list.append(spo2_value)  # füge den empfangenen Wert der Liste hinzu

        # wenn alle Werte empfangen wurden, erstelle einen Pandas DataFrame und gib ihn aus
        if len(spo2_list) == 100:
            pythonDaten = pd.DataFrame(spo2_list)
            print(pythonDaten.head())
            wb.save(filename)


    async def handle_uuid2_notify(self, sender, data):

        ir_values = numpy.frombuffer(data, dtype=numpy.uint32)
        print("IR: {0}".format(ir_values))
        for ir_value in ir_values:
            if ir_value:
                table.add_row(["", ir_value])
                ws.append(["", ir_value])  # Zeile hinzufügen
                ir_list.append(ir_value)  # füge den empfangenen Wert der Liste hinzu


        # wenn alle Werte empfangen wurden, erstelle einen Pandas DataFrame und gib ihn aus
        if len(ir_list) == 100:
            pythonDaten2 = pd.DataFrame(ir_list)
            print(pythonDaten2.head())
            wb.save(filename)

    async def read_data(self):
        UUID1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
        UUID2 = "beb5483e-36e1-4688-b7f5-ea07361b26a9"
        async with BleakClient(self.address) as client:
            await client.start_notify(UUID1, self.handle_uuid1_notify)
            await client.start_notify(UUID2, self.handle_uuid2_notify)
            while True:
                await asyncio.sleep(1)

    def run(self):
        asyncio.run(self.read_data())


class TkinterThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        tkFenster = Tk()
        tkFenster.title('BLE MAX30102')
        tkFenster.mainloop()


def start_threads():
    ble_thread = BLEThread(address)
    tkinter_thread = TkinterThread()

    ble_thread.start()
    tkinter_thread.start()


if __name__ == '__main__':
    start_threads()

wb.save(filename)
But at the moment i also need to synchonizat them, the data are not side by side.
Reply


Messages In This Thread
RE: add svg to 2 thread program and display tabel in GUI - by Nietzsche - May-04-2023, 02:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 46,643 May-06-2023, 08:14 AM
Last Post: pramod08728
  Writing on an lcd display gives problems to the program Idontknowany 1 1,502 Nov-15-2021, 10:46 PM
Last Post: Larz60+
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,373 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,971 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020