Noob needs help. Can't reset paint app after saving image.

22 views
Skip to first unread message

Olivier

unread,
Apr 25, 2017, 2:56:51 AM4/25/17
to Kivy users support
Hi,

I'm creating a signature application and can't seem to reset the screen after saving the screenshot.
Here is a part of my code :

#*** 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"

I've basically lost a week trying all I could find on the web with no success.
Could anybody help me ? Using python 2.7 and kivy 1.9.1 & 1.9.2 (two computers)
Thanks!

ZenCODE

unread,
Apr 25, 2017, 4:34:19 AM4/25/17
to Kivy users support
Does the painter.canvas.clear() not work?

Olivier

unread,
Apr 25, 2017, 4:39:52 AM4/25/17
to kivy-...@googlegroups.com
I don't understand ? Can you be more specific ?

niavlys

unread,
Apr 25, 2017, 6:30:49 AM4/25/17
to Kivy users support
You're clearing your canvas only once, when your widget is initialized;
you should instead clear it every time you enter the Signature screen.

something like

<Signature>:
    name: "signature"
    id: signatureScreen
    on_pre_enter:
        sign.canvas.clear()
    BoxLayout:
        Painter
            id: sign

        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"
   


should do the job.

Olivier

unread,
Apr 25, 2017, 7:02:26 AM4/25/17
to Kivy users support

Thanks a million, niavlys. I owe you a beer, I would never have found that by myself !
Reply all
Reply to author
Forward
0 new messages