community icon indicating copy to clipboard operation
community copied to clipboard

Setting window minimized on app startup gives error on app closure preventing graceful app termination

Open uzbeki opened this issue 4 years ago • 1 comments

Software Versions

  • Python: 3.9.4 (tags/v3.9.4:1f2e308, Apr 4 2021, 13:14:17) [MSC v.1928 32 bit (Intel)] on win32
  • OS: Windows 10 Pro (21H1 version)
  • Kivy: 2.0.0
  • Kivy installation method: pip install kivy[full]

Describe the bug Normally, Kivy closes gracefully with no traceback or error information on the console. But recently, I added a functionality that starts up the kivy app with window minimized: There are two ways to do it (either gives out an error when app is closed):

  1. calling the Window.minimize() function
  2. Setting kivy Config like this: Config.set('graphics', 'window_state', 'minimized')

No problem on window being minimized, but when I close the app using any of the above methods enabled, Kivy consoles out this error message:

 Traceback (most recent call last):
   File "C:\Users\User\Desktop\Project\main_app\main.py", line 501, in <module>        
     mainApp.run()
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\app.py", line 950, in run
     runTouchApp()
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\base.py", line 584, in runTouchApp
     stopTouchApp()
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\base.py", line 625, in stopTouchApp
     EventLoop.close()
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\base.py", line 186, in close
     self.stop()
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\base.py", line 198, in stop
     provider.stop()
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\input\providers\wm_pen.py", line 114, in stop
     SetWindowLong_WndProc_wrapper(self.hwnd, self.old_windProc)
   File "C:\Users\User\Desktop\Project\main_app\.env\lib\site-packages\kivy\input\providers\wm_common.py", line 112, in _closure
     oldAddr = func(hWnd, GWL_WNDPROC, cast(wndProc, c_void_p).value)
 ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type

Expected behavior Close gracefully without error and traceback

To Reproduce Here is the code to reproduce this issue:

from kivy.app import App
from kivy.uix.button import Button
from kivy.config import Config
Config.set('graphics', 'window_state', 'minimized')


class TestApp(App):
    def build(self):
        return Button(text='Hello World')

TestApp().run()

Additional context Here is the link to the same StackOverflow question: Kivy: Running the Kivy app with window minimized triggers an error on app closure. Why?

uzbeki avatar Jun 18 '21 01:06 uzbeki

I'm not sure on the implications of this change fully, so I'm afraid to post a PR. If any Kivy devs are interested in a simple fix, it just looks like an issue of None being passed to a ctypes function. Changing wm_common.py from:

def SetWindowLong_WndProc_wrapper_generator(func):
    def _closure(hWnd, wndProc):
        oldAddr = func(hWnd, GWL_WNDPROC, cast(wndProc, c_void_p).value)
        return cast(c_void_p(oldAddr), WNDPROC)

    return _closure

to

def SetWindowLong_WndProc_wrapper_generator(func):
    def _closure(hWnd, wndProc):
        wndProc = cast(wndProc, c_void_p).value
        if wndProc is None:
            return cast(c_void_p(), WNDPROC)
        
        oldAddr = func(hWnd, GWL_WNDPROC, wndProc)    
        return cast(c_void_p(oldAddr), WNDPROC)

    return _closure

fixes it for me and doesn't appear to cause any side effects.

dfraska avatar Sep 09 '22 02:09 dfraska

Implementing this from Kivy 2.2.0 raises this exception for me:

\lib\site-packages\kivy\lang\builder.py", line 925, in _build_canvas
    setattr(instr, key, value)
  File "kivy\graphics\vertex_instructions_line.pxi", line 790, in kivy.graphics.vertex_instructions.Line.width.__set__
kivy.graphics.vertex_instructions.GraphicException: Invalid width value, must be > 0

This issue comes up when I try to import the Window module:

from kivy.config import Config
Config.set('graphics', 'window_state', 'hidden')
from kivy.core.window import Window
Config.set('graphics', 'window_state', 'visible')

However only in my main application. On a scratch file, it works just fine. Later in my main application, I am modifying some Window properties, but I set the window state to visible right after importing Window, so I have no idea why this error is coming up, and why it isn't coming up in the scratch file.

I just want the kivy window(s) to not open when starting the app up. It's weird because I'm pretty sure it only happens on Windows, not Mac.

This happens at startup btw and crashes the app, not when closing like OP's code.

Fretless14 avatar Jul 09 '23 23:07 Fretless14