树莓派上Kivy / python应用程序中的屏幕截图问题

时间:2018-10-12 08:32:18

标签: python raspberry-pi kivy screenshot

我正在尝试截取我的Kivy绘画应用程序中绘制的线条的屏幕截图。我尝试了以下三种方法,但没有一种起作用。

self.export_to_png('image_1.png')
ImageGrab.grab_to_file('image_2.png')
self.screenshot('image_3.png')  

Export_to_png产生以下图像:

enter image description here

ImageGrab生成kivy应用程序下方内容的打印屏幕:

enter image description here

我不知道执行屏幕截图功能的语法,所以它只会产生错误:

AttributeError: 'Screenshot' object has no attribute 'screenshot'

Python文件:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen , ScreenManager

import pyscreenshot as ImageGrab

class Home(Screen):
    pass

class DrawInput(Widget):

    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)

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

class Screenshot(Widget):   

    def take(self):
        self.export_to_png('image_1.png')
        ImageGrab.grab_to_file('image_2.png')
        self.screenshot('image_3.png')  

class SimpleKivy(App):
    def build(self):
        return

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

Kivy文件:

<Button>:
    font_size: 40
    color: 1,1,1,1

ScreenManager:

    Home:

        name: 'home'
        DrawInput:
            id: widget_clear

        Screenshot:
            id: widget_screenshot

        FloatLayout:

            Button:
                text: "Clear"
                pos_hint: {"x": 0, 'top': 0.6666}
                size_hint: 0.2, 0.3333
                on_release: 
                    widget_clear.clear_canvas()

            Button:
                text: "Save"
                pos_hint: {"x": 0.8, 'top': 0.6666}
                size_hint: 0.2, 0.3333
                on_release:
                    widget_screenshot.take()
                    widget_clear.clear_canvas() 

我正在使用Raspbian 9.4,Python 2.7.13和Kivy 1.10.1。

0 个答案:

没有答案