Kivy截图一个Layout或id
Kivy screenshot a Layout or id
所以我有一个小应用程序,我想截取整个 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)
有两种方法可以做到这一点。直接来自 kvlang,或者像您尝试的那样,使用 python.
中的方法
我将向您展示这两个示例。
直接来自 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()
使用方法:
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()
所以我有一个小应用程序,我想截取整个 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)
有两种方法可以做到这一点。直接来自 kvlang,或者像您尝试的那样,使用 python.
中的方法
我将向您展示这两个示例。
直接来自 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()
使用方法:
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()