[Python] kivy widget draws on root instead of widget place

Discussão em 'Python' iniciado por Stack, Novembro 27, 2025.

  1. Stack

    Stack Membro Participativo

    as my first kivy adventures i try to print a data-graph.

    my main.py looks like this.

    I have to add some more details until stackoverflow lets me ask. But i think the code speaks for itself.

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.widget import Widget
    from kivy.graphics import Line, Color
    from kivy.properties import ListProperty
    import os

    class CamMeasureWidget(BoxLayout):
    def load_file(self):
    """Test: Datei laden und visualisieren"""

    try:
    with open("daten.bin", "rb") as f:
    data = f.read()
    self.ids.plot.data = list(data)
    print(f"{len(data)} Bytes geladen.")
    except Exception as e:
    print("Fehler beim Laden:", e)




    class BytePlot(Widget):
    data = ListProperty([]) # Liste von Bytes

    def __init__(self, **kwargs):
    super().__init__(**kwargs)
    with self.canvas:
    Color(0, 1, 0, 1)
    Line(circle=(self.width / 2, self.height / 2, 25), width=2)
    Line(rectangle=(0, 0, self.width, self.height), width=5)

    def on_data(self, instance, value):
    """Neuzeichnen wenn Daten geändert werden."""
    self.draw_data()

    def draw_data(self):
    self.canvas.clear()

    if not self.data:
    return

    with self.canvas:
    Color(0, 1, 0) # grün

    width = self.width
    height = self.height
    n = len(self.data)

    if n == 0:
    return

    # Abstand zwischen Linien
    x_step = width / n

    for i, b in enumerate(self.data):
    x = i * x_step
    line_height = (b / 255) * height
    Line(points=[x, 0, x, line_height], width=1)

    class CamMeasureApp(App):
    def build(self):
    return CamMeasureWidget()



    if __name__ == '__main__':
    CamMeasureApp().run()


    here is the kivy file

    #:kivy 1.0.9

    <CamMeasureWidget>:
    # BoxLayout:
    orientation: "vertical"
    spacing: 10
    padding: 10

    BytePlot:
    id: plot

    Button:

    text: "Start (Datei laden)"
    on_release: root.load_file()

    Button:
    text: "Stopp"
    on_release: print("Stopp gedrückt")


    The green graph should be drawed in the first third, but it is drawn on background starting at lower left edge.

    What is wrong here?

    Screenshot

    Greets from Bavaria

    Continue reading...

Compartilhe esta Página