Search code examples
kivykivy-language

importing Window from kivy.core.window opens a second Kivy window that crashes


I'm trying to import from kivy.core.window import Window in order to enable texture support to display an image from a numpy array (see https://groups.google.com/forum/#!topic/kivy-users/2Hvarxhz6lU).

However, when I run the import, Kivy attempts to open another window which is blank and unresponsive. When I remove the import statement, the app launches as normal.

Can anyone point me in the right direction to import Window to allow Texture support without this behaviour?

Thanks,

Oliver.


Solution

  • That is highly expected because Kivy is built on this importing behavior. On a simple Window import e.g. in a console:

    >>> from kivy.core.window import Window
    

    a blank OpenGL window is created and it waits for further instructions such as first drawing and other initialization stuff.

    Let's look at the code from the mailing list:

    class MainConsole():
        def build(self):
            texture = Texture.create()  # no window, boom
    if __name__ == '__main__':
        MainApp().run()  # except other stuff also creates a window
    

    To make this actually work you will have to create the texture after the Window is created i.e. this:

    class MainConsole():
        def build(self):
            from kivy.core.window import Window
            texture = Texture.create()  # window, no boom
    

    The struggle might happen if you try to do that in a Thread(not sure) or with multiprocessing(most likely) if you try to do something like:

    def blob():
        from kivy.core.window import Window
        texture = Texture.create()
    

    if you run this function in a separate process, the separate process won't have the same Window, therefore it creates a new one with the Window import and here you are.

    To fix it you can do two things:

    • use Config and move the Window somewhere out of the viewing area with top and left
    • create and assemble the texture within the main Window