Kivy截图布局或ID

时间:2017-02-25 15:03:55

标签: android kivy python-3.5 kivy-language

所以我有一个小应用程序,我想做一个整个BoxLayout的屏幕截图,并省略其他部分的父布局。

注意:这适用于Android应用

基本上我有这样的事情:

    BoxLayout:
        id: image_area
        size_hint_y: 600
        Image:
            source: root.image_source
            size: self.size
            DragText:
                background_color: (0, 0, 0, 0)
                foreground_color: (255,255,255,255)
                multiline: True
                height: self.minimum_height
                width: '400dp'
                center: self.parent.center
                text: 'Before'
                font_size: '60px'
        Image:
            source: root.image_source2
            DragText:
                background_color: (0, 0, 0, 0)
                foreground_color: (255,255,255,255)
                multiline: True
                height: self.minimum_height
                width: '400dp'
                center: self.parent.center
                text: 'After'
                font_size: '60px'

我在这个上面有其他布局,甚至父母也是一个盒子布局,但我只是想截取这个布局,而且我遇到了麻烦。

我试过了:

def screenshot(self, widget):
    widget.export_to_png('{0}.png'.format(datetime.now()))

但它不起作用,任何想法我怎么能做到这一点?

我忘了指定这个,激活屏幕截图的按钮看起来像这样

        Button:
            size_hint_x: 2
            text: 'Save'
            on_release: root.screenshot(image_area)

1 个答案:

答案 0 :(得分:1)

There are two ways to do this. Directly from kvlang, or like you tried, with a method in python.
I will show you both examples.

Directly from kvlang:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''


<MyLayout>:
    orientation: "vertical"
    Label:
        text: "Label1 in outer box"

    BoxLayout:
        id: myexport
        Label:
            text: "Label in inner layout"

    Label:
        text: "Label2 in outer box"
    Button:
        text: "Button in outer, to export"
        on_release: myexport.export_to_png("test.png")


''')

class MyLayout(BoxLayout):
    pass


class MyApp(App):
    def build(self):
        return MyLayout()


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

With method:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''


<MyLayout>:
    orientation: "vertical"
    Label:
        text: "Label1 in outer box"

    BoxLayout:
        id: myexport
        Label:
            text: "Label in inner layout"

    Label:
        text: "Label2 in outer box"
    Button:
        text: "Button in outer, to export"
        on_release: root.export()


''')

class MyLayout(BoxLayout):

    def export(self,*args):
        self.ids.myexport.export_to_png("test2.png")


class MyApp(App):
    def build(self):
        return MyLayout()


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