Echtzeit Graph kivy kvfile

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Modjteba
User
Beiträge: 18
Registriert: Donnerstag 3. August 2023, 11:12

Code: Alles auswählen

def data_value():
    x_value = []
    y_value = []
    dataUnits = MyDll.DataUnits
    strDataUnits = MyDll.UnitsDecoder(dataUnits)
    for i in range(1, 20):
        data = MyDll.DataValue
        print('Zyklus ' + str(i) + ' Value: ' + str(data) + ' ' + str(strDataUnits))
        time.sleep(0.5)
        x_value.append(data)
        y_value.append(i)
    print(x_value)
    print(y_value)
    dataBase = MyDll.BaseValue
    print('BaseValue: ' + str(dataBase))
    return x_value, y_value
Hallo zsm,
ich bin neuling in kivy.
ich möchte die funktion oben in kivy als echtzeit Graph darstellen. wie könnte ich das erreichen. bin über tipps und Hilfe dankbar.

die daten wird von einem BLE gerät ermittelt.
danke im Voraus.
__deets__
User
Beiträge: 14545
Registriert: Mittwoch 14. Oktober 2015, 14:29

Mit sleep kann man in einer GUI nichts erreichen. Die ist Ereignisbasiert. Du musst einen Timer benutzen, dier die Daten stueckweise fuettert. Graphen in Kivy zB via https://github.com/kivy-garden/graph
Modjteba
User
Beiträge: 18
Registriert: Donnerstag 3. August 2023, 11:12

__deets__ hat geschrieben: Donnerstag 21. September 2023, 11:39 Mit sleep kann man in einer GUI nichts erreichen. Die ist Ereignisbasiert. Du musst einen Timer benutzen, dier die Daten stueckweise fuettert. Graphen in Kivy zB via https://github.com/kivy-garden/graph

danke für die antwort.

main py

Code: Alles auswählen

class Graph(Screen):
    def build(self):
        box = BoxLayout(orientation='vertical')
        a = Button(text='Go Back', height=40, size_hint_y=None)
        self.graf = plt.gcf()
        plt.ylabel('Time')
        plt.xlabel('data')
        box.add_widget(FigureCanvasKivyAgg(self.graf))
        box.add_widget(a)
        Clock.schedule_interval(self.data_value, 1 / 25)
        return box

    def data_value(self, *args):
        self.x = [2, 4, 6, 8, 10, 12]
        self.y = [5, 10, 15, 20, 25, 30]
        # for i in range(1, 15):
        # data = MyDll.DataValue
        # self.x.append(data)
        # self.y.append(i)
        plt.plot(self.x, self.y)
        self.graf.canvas.draw()

    pass
ich habe die funktion in der klasse geschrieben, wollte erstmal die werte manuell in graph darstellen.

kv file

Code: Alles auswählen

<Graph>
    transition: SlideTransition()
    canvas:
        Color:
            rgba: 1, 2, 1, 1
        Rectangle:
            size: self.size
            pos: self.pos

    Label:

        pos_hint:{"top": .95, "left": 1}
        size_hint: 1, .1
        text:"Graph to B24"
        color: 135, 206, 250, 1
        font_size: 34

    ImageButton:
        pos_hint:{"center_x": 0.5, "center_y": .1}
        size_hint: None, .1
        source: "img/R.png"
        on_press: app.change_screen("home_screen")
mein frage ist :

wie konnte ich den graph in mein kv file einbinden und graph anzeigen lassen.
bin sehr dankbar über eure hilfe.
Modjteba
User
Beiträge: 18
Registriert: Donnerstag 3. August 2023, 11:12

hallo zsm,
ich versuche mit kivy eine echtzeit graph auf bildschirm zubekommen, bekomme leider fehler.
mein ziel ist mit einem Button klick soll die IOT gerät die daten in graph darstellen.
wenn die den code ausführe und den button klicke zeigt mir keine graph und das program bericht mit error zsm.

Code: Alles auswählen

class GraphScreen(Screen):

    def __int__(self, **kwargs):
        super(GraphScreen, self).__int__(**kwargs)
        self.graph = self.ids.graph_b24
        self.plot = MeshLinePlot(color=[1, 0, 0, 1])
        self.x_value = []
        self.y_value = []

    def start(self):
        self.graph.add_plot(self.plot)
        Clock.schedule_interval(self.get_value, 0.001)

    def stop(self):
        Clock.unschedule(self.get_value)

    def get_value(self):
        for i in range(1, 100):
            data = MyDll.DataValue
            self.x_value.append(data)
            self.y_value.append(len(self.x_value))
            self.plot.points = [(self.y_value[i], self.x_value[i]) for i in range(len(self.x_value))]


kv File :

Code: Alles auswählen

#:import MeshLinePlot kivy.garden.graph.MeshLinePlot
<GraphScreen>:
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            size_hint: [1, .8]
            Graph:
                id: graph_b24
                x_label: ""
                y_label: "Level"
        BoxLayout:
            size_hint: [1, .2]
            orientation: "horizontal"
            Button:
                text: "START"
                bold: True
                on_press: root.start()
            Button:
                text: "STOP"
                bold: True
                on_press: root.stop()
Benutzeravatar
grubenfox
User
Beiträge: 447
Registriert: Freitag 2. Dezember 2022, 15:49

und welcher Error (mit kompletten Stacktrace bitte)?
Ach, und was ist 'MyDll'?
Modjteba
User
Beiträge: 18
Registriert: Donnerstag 3. August 2023, 11:12

MYDLL ist ein classe von der DLL datei von IOT gerät, mit DataValue wird die daten berechnet, d.h. wenn ich an sensor recht oder links schraube, wird mir neue werten berechnet. und die werte möchte ich in graph dastellen.


#### Add Dll Data using pythonnet bibliothek
import clr
clr.AddReference("Dll/B24Lib")
from B24Lib import BGLib
MyDll = BGLib()



####Error :

Traceback (most recent call last):
File "C:\Users\mmohammadi\PycharmProjects\Hycat\main.py", line 103, in <module>
MainApp().run()
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\app.py", line 956, in run
runTouchApp()
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\base.py", line 574, in runTouchApp
EventLoop.mainloop()
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\base.py", line 339, in mainloop
self.idle()
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\base.py", line 383, in idle
self.dispatch_input()
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\base.py", line 334, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\base.py", line 263, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\core\window\__init__.py", line 1691, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\core\window\__init__.py", line 1708, in on_touch_down
if w.dispatch('on_touch_down', touch):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\screenmanager.py", line 1210, in on_touch_down
return super(ScreenManager, self).on_touch_down(touch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\relativelayout.py", line 306, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
File "C:\Users\mmohammadi\PycharmProjects\Hycat\venv\Lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Users\mmohammadi\PycharmProjects\Hycat\kv\graph.kv", line 13, in <module>
on_press: root.start()

File "C:\Users\mmohammadi\PycharmProjects\Hycat\main.py", line 63, in start
self.graph.add_plot(self.plot)
^^^^^^^^^^
AttributeError: 'GraphScreen' object has no attribute 'graph'

Process finished with exit code 1
Antworten