当 Kivy window 未最小化时如何 运行 函数

How to run a function when the Kivy window is un-minimised

我有一个可以透视的应用程序 window-

当它未最小化时,我想截屏以更新图像,但我不知道如何在 window 未最小化时 运行 一个函数。

我试过了

from io import BytesIO

from PIL import ImageGrab
from kivy.lang.builder import Builder
from kivy.clock import Clock
from kivy.core.image import Image as CoreImage
from kivy.core.window import Window as CoreWindow
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App


class Widget(FloatLayout):
    def __init__(self, *args, **kwargs):
        super(Widget, self).__init__(*args, **kwargs)

        CoreWindow.bind(left=self.on_pos, top=self.on_pos)

        Clock.schedule_once(self.take_screenshot, 0)

    def on_pos(self, _, pos):
        self.ids["parentScreenImage"].pos = 0 - CoreWindow.left, \
                                            (CoreWindow.top + CoreWindow.height) - self.ids["parentScreenImage"].size[1]

    def take_screenshot(self, _=None):
        img = ImageGrab.grab()
        data = BytesIO()
        img.save(data, format='png')
        data.seek(0)
        img = CoreImage(BytesIO(data.read()), ext='png')

        self.ids["parentScreenImage"].texture = img.texture
        self.ids["parentScreenImage"].size = img.size
        self.ids["parentScreenImage"].pos = 0 - CoreWindow.left, 0 - CoreWindow.top


class app(App):
    def on_unminimise(self):
        print("worked with on_unminimise")

    def on_un_minimise(self):
        print("worked with on_un_minimise")

    def on_resume(self):  # I think this only works on a phone
        print("worked with on_resume")

    def build(self):
        return Widget()


Builder.load_string("""
<Widget>:
    Image:
        id: parentScreenImage

        size_hint: None, None
""")
app().run()

但没有打印任何内容。 google 上似乎也没有任何内容。请帮忙。

P.S。出于兴趣,当 window 最小化

时,你会如何 运行

我认为你可以绑定到 on_show 事件:

class app(App):
    def on_show(self, window):
        print('show', window)

    def build(self):
        CoreWindow.bind(on_show=self.on_show)
        return Widget()