Repository: kivy/kivy

Issue:
Resizing in Debug (PyCharm) crashes with "NameError: name 'EventLoop' is not defined" in kivy\core\window\window_sdl2.py
**Software Versions**
* Python: 3.10.4
* OS: Windows 11
* Kivy: 2.1.0
* Kivy installation method: Pycharm (pip?)
* PyCharm 2021.3.2 (Community Edition)

**Describe the bug**
Even the most basic Kivy App crashes when started as "debug" in PyCharm. When "run" in PyCharm, resizing works, but it's not possible to debug anything when you resize.  (most likely at least in Windows because its the windows_sdl2.py)

 Traceback (most recent call last):
   File "[...]\lib\site-packages\kivy\core\window\window_sdl2.py", line 270, in _event_filter
     EventLoop.idle()
 NameError: name 'EventLoop' is not defined"

**Expected behavior**
No crash, debugging should just work after resize.

**To Reproduce**
Create any Kivy App in PyCharm, start the program as debug (with or without break point) and resize the window. -> crash

**Code and Logs and screenshots**

```python
from kivy.app import App


class KivoApp(App):

    def build(self):
        pass


if __name__ == '__main__':
    KivoApp().run()

```

**Additional context**



Discussion:
I see the same behavior under Ubuntu 22.04 with PyCharm 2022.1 (Community Edition)
Kivy 2.1.0 also crashes for me on Ubuntu 22.04 und OS X 12 Montery when either trying to close or resize a window because "EventLoop is None" in more or less every call of _event_filter. I am also using PyCharm.

When running it in Release Mode the Window is resizable but closing the application never finishes.
Same on Ubuntu 22.04 PyCharm 2022.2.1 (Professional Edition)
I am having the same problem on macOS Monterey 12.6 (chip M1)
Python 3.10.6
Kivy 2.1.0
Have the same problem on Manjaro with PyCharm 2022.2.2 (Community Edition).
Python 3.10.7
Kivy 2.1.0

Error:
```
 Traceback (most recent call last):
   File "/home/doopath/Files/code/Kivy/kivy_venv/lib/python3.10/site-packages/kivy/core/window/window_sdl2.py", line 270, in _event_filter
     EventLoop.idle()
 NameError: name 'EventLoop' is not defined
python-BaseException
```
Same issue here.

PyCharm 2022.2.2 (Professional Edition).
Python 3.10.7
Kivy 2.1.0

```
 Traceback (most recent call last):
   File "[xxxxxxxx]\venv\lib\site-packages\kivy\core\window\window_sdl2.py", line 270, in _event_filter
     EventLoop.idle()
 NameError: name 'EventLoop' is not defined
```
It seems i fix this (temporary and it's not relieable).
Debugger shows me 2 problems:

1. window_sdl2.py file, WindowSDL._event_filter method. I rewrite the last condition like this:
```python3
elif action == 'windowresized':
    self._size = largs
    self._win.resize_window(*self._size)
    # Force kivy to render the frame now, so that the canvas is drawn.
    try: EventLoop.idle()
    except NameError: pass
```
2. window_sdl2.py file, WindowSDL.mainloop method. I rewrite the condition placed at ~570 line like this:
```python3
if action == 'quit':
    if self.dispatch('on_request_close'):
        continue
    try: EventLoop.quit = True
    except NameError: exit()
    break
```
Dunno if these changes will brake the freck out the kivy module, but now it seems to work nicely.
Hope this issue will be resolved soon.
I looked up a bit. Somehow when a window is resized, mainloop in window_sdl2.py looses its scope on EventLoop. 
@doopath solution did not work for me, the window kept open. 

Importing EventLoop again in the if condition will help, but this is not a nice fix: 
```python
if action == 'quit':
    from kivy.base import EventLoop
    if self.dispatch('on_request_close'):
        continue
    EventLoop.quit = True
    break
```

Edit: The method  __event_filter_ in window_sdl2.py is called when resizing the window. After process EventLoop.idle() (last elif condition in method) it looses its scope on it. I do not now why yet. 
I noticed that if you place a breakpoint just before the place this error occurs, EventLoop is defined and if you step over the code it behaves as expected.

I would imagine it's a PyCharm quirk and I've seen the debugger doing some weird things before.

EDIT: scratch that, EventLoop is defined after one iteration, but the resizing is triggering another iteration and in that iteration it's not defined.
Looking at window_sdl2.py again, I noticed EventLoop is imported within the create_window function in the WindowSDL class:

```python
# auto add input provider
Logger.info('Window: auto add sdl2 input provider')
from kivy.base import EventLoop
SDL2MotionEventProvider.win = self
EventLoop.add_input_provider(SDL2MotionEventProvider('sdl', ''))
```

I don't really understand why EventLoop is imported **again** in that module, seeing as it's already imported at the top of the file.

**When I remove that import it fixes the problem.**

I have no idea if that has any other effects and if that import has any reason to be there.
^ I can confirm the fix mentioned above ^ and I suggest this simple fix:
~~from kivy.base import EventLoop~~

Can we get a PR?
Yes, removing that line solves the problem for me too 
removing that line fixed the problem for me too (OP) 

Resolving patch:
diff --git a/kivy/core/window/window_sdl2.py b/kivy/core/window/window_sdl2.py
--- a/kivy/core/window/window_sdl2.py
+++ b/kivy/core/window/window_sdl2.py
@@ -367,7 +367,6 @@ def create_window(self, *largs):
 
         # auto add input provider
         Logger.info('Window: auto add sdl2 input provider')
-        from kivy.base import EventLoop
         SDL2MotionEventProvider.win = self
         EventLoop.add_input_provider(SDL2MotionEventProvider('sdl', ''))
 


Test patch: