Reputation: 181
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
Reputation: 16041
pointsXY=[]
App.get_running_app()
pointsXY=[]
with App.get_running_app().graphListOfXY
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()
graphInput.graphListOfXY = graphInput.canvasDrawing.pointsXY
import graphInputKivy
if __name__ == '__main__':
global graphInput
graphInput = graphInputKivy.GraphInputKivyApp()
graphInput.run()
print(f"\nmain: graphInput.graphListOfXY")
print(graphInput.graphListOfXY)
Upvotes: 2