kivy - 保存图像后清除屏幕?

时间:2017-04-25 06:14:24

标签: python-2.7 kivy

使用kivy 1.9.1 / 2

我想要实现的目标:

点击'确定'保存图像然后清除屏幕。因此,当我下次进入“签名”屏幕时,除了“确定”之外,屏幕不包含任何内容。按钮。

我的问题:
canvas.clear不起作用。我可能以错误的方式整合它。 有人可以帮忙吗? 我是一个初学者,并且已经坚持了好几天。 提前谢谢。

#*** PYTHON FILE
from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Line
import datetime
Config.set('graphics', 'width', '1024')
Config.set('graphics', 'height', '768')

class Other(Screen):
        pass
class Painter(Widget):
        def __init__(self,**kwargs):
                super(Painter, self).__init__(**kwargs)
                self.canvas.clear()
        def on_touch_down(self, touch):
                with self.canvas:
                        touch.ud['line'] = Line(points=(touch.x, touch.y))
        def on_touch_move(self, touch):
                touch.ud['line'].points += [touch.x, touch.y]

class Signature(Screen):
            def Save_image(self):
                    from PIL import Image, ImageFont, ImageDraw
                    self.export_to_png("test.png")
                    img=Image.open("test.png")
                    draw=ImageDraw.Draw(img)
                    font=ImageFont.truetype("/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf",24, encoding="unic")
                    draw.text((0,0),str("{:%d %b %y-%Hh%Mm%Ss}".format(datetime.datetime.now())), (255,255,255), font=font)
                    img.save("test.png")

class ScreenManagement(ScreenManager):
        pass

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

class MainApp(App):
        def build(self):
                return presentation

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

#***KV FILE
#:import datetime datetime
#:kivy 1.9.1
ScreenManagement:
        Other:
        Signature:
<Other>:
        name: "other"
        id: otherScreen
        BoxLayout:
                Button:
                    text: "OK"
                    on_release: 
                            root.manager.transition.direction = 'right'
                            app.root.current = "signature"

<Signature>:
        name: "signature"
        id: signatureScreen
        BoxLayout:
            Painter
            Button:
                    background_color: 0,0,1,1
                    font_size: 32
                    size_hint: (0.1, 0.1)
                    text: "OK"
                    pos_hint: {"right":1, "bottom":1}           
                    on_release: 
                        root.Save_image()
                        root.manager.transition.direction = 'right'
                        app.root.current = "other"

1 个答案:

答案 0 :(得分:0)

正如niavlys对kivy的google小组所建议的那样,答案是每次加载时清除画布。显而易见,但要实现这一点,必须在屏幕中声明kv文件:

on_pre_enter:
      sign.canvas.clear()
BoxLayout:
      Painter
          id: sign