使用Kivy访问类变量

时间:2019-05-03 22:48:30

标签: python class kivy

我是第一次使用Kivy,在小部件所需的不同类与如何访问其变量之间有些困惑。我有一个主模块,从那里我可以启动另一个包含Kivy的模块。然后,我试图从on_touch方法中检索点列表。

主模块:

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

“ Kivy”模块:

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()

我可以使用graphInput.canvasDrawing.pointsXY从on_touch_down方法(当我关闭Kivy窗口时)访问点列表,但是在调用on_touch方法之后如何更新graphInput.graphListOfXY?

谢谢

1 个答案:

答案 0 :(得分:1)

py-Kivy模块

  • 删除类属性pointsXY=[]
  • 使用App.get_running_app()
  • 访问应用对象
  • pointsXY=[]的所有引用替换为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-主模块

  • 删除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)