简体   繁体   中英

Window.screenshot returns black screen on iOS

I am writing simple drawing app in Kivy. It works fine on iPhone and iPad but Window.screenshot() returns only a black screen. What did I wrong? Also, is there a way so that the screenshot() gets saved directly on dropbox/iCloud/Files-App?


    class DrawInput(Widget):

    def btn_save(self):


        user_data_dir = App.get_running_app().user_data_dir

        name = join(user_data_dir, "filename.png")

        Window.screenshot(name)     

    def on_touch_down(self, touch):
        with self.canvas:
            Color(0, 0, 0)
            touch.ud["line"] = Line(points = (touch.x, touch.y))

    def on_touch_move(self, touch):
        touch.ud["line"].points += (touch.x, touch.y)

    def on_touch_up(self, touch): 
        pass

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

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

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

Kivy:-


    Screen:

      name: "drawing"
      on_pre_enter: drawing.canvas.clear()

      FloatLayout:

         DrawInput:
            id: drawing
         Button:
            text: "finish"
            on_press: drawing.btn_save()

I expect to get a screenshot of the drawing.

I can't check your code for iOS right now, (I will try to do it a little later.) but on Linux (Ubuntu) it works fine: the user can take a screenshot of the entire application area, the image is saved in png format in the source folder (unfortunately, I didn't quite get along with your output paths , therefore, I replaced the name value with name = 'test.png' ).

then I wrote a minimal example that also works on my system:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window

KV = '''
Button:
    text: 'Save'
    on_press: app.btn_save()
'''

class ApplePenApp(App):

    def build(self):
        self.root = Builder.load_string(KV)

    def btn_save(self):
        Window.screenshot('test.png')

ApplePenApp().run()

Please check if it works on your system (if it works, then most likely the problem is somewhere in your code).

I found the black screenshot problem only when doing something like this:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window

KV = """
Button
    text: '123456'
"""

class MyApp(App):
    def build(self):
        self.root = Builder.load_string(KV)
        self.make_screenshot()

    def make_screenshot(self):
        Window.screenshot('test.png')

MyApp().run()

However I also saw problems with Window.screenshot with some users, for example: https://github.com/kivy/kivy/issues/4514

Btw, as a more advanced alternative to Window.screenshot , you can try export_to_png . You can use this method for any widget, here is a short example:

from kivy.app import App
from kivy.lang import Builder

KV = '''
Button:
    text: 'Save'
    on_press: app.btn_save(self)
'''

class ApplePenApp(App):

    def build(self):
        self.root = Builder.load_string(KV)

    def btn_save(self, inst):
        inst.export_to_png('test.png')

ApplePenApp().run()

You cannot use it for a window, but you can use it for a screen (in your case it can be SecondScreen "drawing"):

from kivy.app import App
from kivy.lang import Builder

KV = '''
ScreenManager
    Screen
        id: screen
        Button:
            text: 'Save'
            on_press: app.widget_save(screen)
'''

class ApplePenApp(App):

    def build(self):
        self.root = Builder.load_string(KV)

    def widget_save(self, inst):
        inst.export_to_png('test.png')

ApplePenApp().run()

please check if these examples work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM