用其他类的kivy语言清洁画布

时间:2017-09-27 12:10:26

标签: python kivy

我刚开始使用Kivy作为我的应用程序而且我遇到了问题。 我尝试清理我的画布,但我不能将按钮与画布相关联

class DrawInput(Widget):
def on_touch_down(self, touch):
    print(touch)
    with self.canvas:
        touch.ud["line"] = Line(points=(touch.x, touch.y), width=100)

def on_touch_move(self, touch):
    #print(touch)
    touch.ud["line"].points += (touch.x, touch.y)

def on_touch_up(self, touch):
    self.export_to_png("roy.png")
    print("RELEASED!", touch)

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


class AnotherScreen(Screen):
pass

presentation = Builder.load_file("main2.kv")

class MainApp(App):

def build(self):
    return presentation

def clear_canvas(self, obj):

MainApp()。运行()

这是main2.kv

GridLayout:
    cols: 2
    Button:
        on_release: root.change_text()
        color: 0,1,0,1
        font_size: 25
        size_hint: 0.3,0.2
        text: root.random_number
        pos_hint: {"right":1, "top":1}
    DrawInput
    Button:
        on_release: root.clean()
        color: 0,1,0,1
        font_size: 25
        size_hint: 0.3,0.2
        text: "Clear"

我的问题是我需要从其他课程调用Clean方法,但是当我尝试它时,它说我需要发送" Self",有人能帮助我吗? 只是试图清理与DrawInput相关的画布

1 个答案:

答案 0 :(得分:1)

您必须使用ObjectProperty连接DrawInput子窗口小部件以引用clean方法。有关详细信息,请参阅以下示例。

实施例

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty


class DrawInput(Widget):
    def on_touch_down(self, touch):
        print(touch)
        with self.canvas:
            touch.ud["line"] = Line(points=(touch.x, touch.y), width=100)

    def on_touch_move(self, touch):
        #print(touch)
        touch.ud["line"].points += (touch.x, touch.y)

    def on_touch_up(self, touch):
        # self.export_to_png("roy.png")
        self.export_to_png("kivy-logo-black-64.png")
        print("RELEASED!", touch)

    def cleaner(self):
        print("cleaner: ", self)
        self.canvas.clear()


class FirstScreen(Screen):
    draw = ObjectProperty()


class AnotherScreen(Screen):
    pass


class Manager(ScreenManager):
    mgr = ObjectProperty()

    def change_text(self):
        pass

    def random_number(self):
        pass

    def clean(self):
        print("clean: ", self)
        self.mgr.draw.cleaner()


class TestApp(App):

    def build(self):
        return Manager()


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.10.0

<DrawInput>:

<Manager>:
    mgr: first_screen
    FirstScreen:
        id: first_screen
    AnotherScreen:

<FirstScreen>:
    draw: draw_input
    GridLayout:
        cols: 2
        Button:
            on_release: app.root.change_text()
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3, 0.2
            # text: app.root.random_number()
            text: "123"
            pos_hint: {"right":1, "top":1}

        DrawInput:
            id: draw_input

        Button:
            on_release: app.root.clean()
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3, 0.2
            text: "Clear"

<AnotherScreen>:

输出

enter image description here

相关问题