tienow
tienow

Reputation: 181

Accessing class variables using Kivy

I'm using Kivy for the first time and I'm getting a little bit confused between the different classes required for the widget and how to access their variables. I have a main module from where I am launching another module that contains Kivy. I am then trying to retrieve a list of points from the on_touch methods.

Main module:

if __name__ == '__main__':
    global graphInput
    graphInput=graphInputKivy.GraphInputKivyApp()
    graphInput.run()
    graphInput.graphListOfXY = graphInput.canvasDrawing.pointsXY
    print(graphInput.graphListOfXY)

'Kivy' module:

class CanvasDrawing(Widget):
    pointsXY=[]
    def on_touch_down(self, touch):
        with self.canvas:
            Color(1, 1, 0)
            touch.ud['line'] = Line(points=(touch.x, touch.y))
            self.pointsXY=touch.ud['line'].points

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]
        self.pointsXY+= [touch.x, touch.y]


class GraphInputKivyApp(App):
    graphListOfXY=[]
    def build(self):
        layout = Widget()
        self.canvasDrawing=CanvasDrawing()
        clearCanvasButton = Button(text='Clear')
        clearCanvasButton.bind(on_release=self.clear_canvas)
        layout.add_widget(self.canvasDrawing)
        layout.add_widget(clearCanvasButton)
        return layout  

    def clear_canvas(self, obj):
        self.canvasDrawing.canvas.clear()

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

I'm able to access the list of points from the on_touch_down method (when I close the Kivy window) with graphInput.canvasDrawing.pointsXY but how can I update graphInput.graphListOfXY after the on_touch method is called?

Thanks,

Upvotes: 1

Views: 467

Answers (1)

ikolim
ikolim

Reputation: 16041

py - Kivy module

  • Remove class attribute, pointsXY=[]
  • Access app object using App.get_running_app()
  • Replace all references of pointsXY=[] with App.get_running_app().graphListOfXY

graphInputKivy.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Line


class CanvasDrawing(Widget):

    def on_touch_down(self, touch):
        with self.canvas:
            Color(1, 1, 0)
            touch.ud['line'] = Line(points=(touch.x, touch.y))
        App.get_running_app().graphListOfXY.append([touch.x, touch.y])

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y]
        App.get_running_app().graphListOfXY.append([touch.x, touch.y])


class GraphInputKivyApp(App):
    graphListOfXY = []

    def build(self):
        layout = Widget()
        self.canvasDrawing = CanvasDrawing()
        clearCanvasButton = Button(text='Clear')
        clearCanvasButton.bind(on_release=self.clear_canvas)
        layout.add_widget(self.canvasDrawing)
        layout.add_widget(clearCanvasButton)
        return layout

    def clear_canvas(self, obj):
        self.canvasDrawing.canvas.clear()

    def on_stop(self):
        print(f"\GraphInputKivyApp.non_stop: self.graphListOfXY")
        print(self.graphListOfXY)


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

py - Main module

  • Remove graphInput.graphListOfXY = graphInput.canvasDrawing.pointsXY

main.py

import graphInputKivy

if __name__ == '__main__':
    global graphInput
    graphInput = graphInputKivy.GraphInputKivyApp()
    graphInput.run()
    print(f"\nmain: graphInput.graphListOfXY")
    print(graphInput.graphListOfXY)

Upvotes: 2

Related Questions