Why Kivy Library does not work with exec function python?

89 views
Skip to first unread message

husamal...@gmail.com

unread,
Mar 12, 2022, 1:47:55 AM3/12/22
to Kivy users support
I trying to use exec to execute kivy code while kivy app instance running
as in the attached file but Im facing this error

Traceback (most recent call last):
   File "_ctypes/callbacks.c", line 232, in 'calling callback function'
   File "C:\Users\husam\PycharmProjects\hello-world\venv\lib\site-packages\kivy\input\providers\wm_pen.py", line 78, in _pen_wndProc
     hwnd, msg, wParam, lParam)
 ctypes.ArgumentError: argument 3: <class 'RecursionError'>: maximum recursion depth exceeded in __instancecheck__
 Traceback (most recent call last):
   File "_ctypes/callbacks.c", line 232, in 'calling callback function'
   File "C:\Users\husam\PycharmProjects\hello-world\venv\lib\site-packages\kivy\input\providers\wm_pen.py", line 78, in _pen_wndProc
     hwnd, msg, wParam, lParam)
 ctypes.ArgumentError: argument 3: <class 'RecursionError'>: maximum recursion depth exceeded in __instancecheck__
 Traceback (most recent call last):
   File "_ctypes/callbacks.c", line 232, in 'calling callback function'
   File "C:\Users\husam\PycharmProjects\hello-world\venv\lib\site-packages\kivy\input\providers\wm_pen.py", line 78, in _pen_wndProc
     hwnd, msg, wParam, lParam)
 ctypes.ArgumentError: argument 3: <class 'RecursionError'>: maximum recursion depth exceeded in __instancecheck__
 Traceback (most recent call last):
   File "_ctypes/callbacks.c", line 232, in 'calling callback function'
   File "C:\Users\husam\PycharmProjects\hello-world\venv\lib\site-packages\kivy\input\providers\wm_pen.py", line 78, in _pen_wndProc
     hwnd, msg, wParam, lParam)
 ctypes.ArgumentError: argument 3: <class 'RecursionError'>: maximum recursion depth exceeded in __instancecheck__

This error without the app stops only freezing
and this only when Im trying to execute kivy code
main.py

Elliot Garbus

unread,
Mar 12, 2022, 9:49:18 AM3/12/22
to kivy-...@googlegroups.com

Kivy runs an event loop, the event loop is effectively an infinite loop looking for events to process.  You have launched an infinite loop inside an infinite loop.

The kivy instance that you launched the other copy of kivy from is running the main event loop, the  “hello world” is running another event loop.

 

What is it you want to accomplish?

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/c2b5bf1e-8833-4b4e-acaf-111f8cbe1455n%40googlegroups.com.

 

ElliotG

unread,
Mar 12, 2022, 10:34:59 AM3/12/22
to Kivy users support
You might want to look at os.exec* functions.  https://docs.python.org/3/library/os.html#os.execv
Here is an example that restarts a kivy program, you could do something similar to replace the existing program with another program.
Note this may not work if run inside an ide, run this from the terminal.

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

import os
import sys

kv = """
BoxLayout:
    Label:
        text: 'Restart Test'
        font_size: sp(40)
    Button:
        text: 'Press to Restart'
        font_size: sp(40)
        on_release: app.restart()
"""


class RestartTestApp(App):
    def build(self):
        return Builder.load_string(kv)

    @staticmethod
    def restart():
        print(f'exec: {sys.executable} {["python"] + sys.argv}')
        os.execvp(sys.executable, ['python'] + sys.argv)


RestartTestApp().run()

Abdoh Fawze

unread,
Mar 12, 2022, 11:04:32 AM3/12/22
to kivy-...@googlegroups.com

Elliot Garbus

unread,
Mar 12, 2022, 11:16:24 AM3/12/22
to kivy-...@googlegroups.com

husamal...@gmail.com

unread,
Mar 12, 2022, 11:17:37 AM3/12/22
to Kivy users support
I want to execute a python code but if the code is kivy my app freezing 
Its possible to execute kivy and python code with  os.execvp?

Elliot Garbus

unread,
Mar 12, 2022, 11:24:31 AM3/12/22
to kivy-...@googlegroups.com

Yes.  Look at what the reset code I sent is doing.  It is launching a new program. 

Put your ‘child’ kivy program in a separate file, and use the appropriate os.exec to run it.

Elliot Garbus

unread,
Mar 12, 2022, 12:11:15 PM3/12/22
to kivy-...@googlegroups.com

Here is an example:

Main.py

 

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

import os
import sys

kv =
'''
Button:
    text: 'launch hello'
    on_press:app.go()
'''


class TestApp(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def go(self):
        os.execlp(sys.executable,
'python', 'hello.py')


TestApp().run()

 

Hello.py:

 

from kivy.app import App
from kivy.uix.button import Button



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


TestApp().run()

Elliot Garbus

unread,
Mar 12, 2022, 12:25:42 PM3/12/22
to kivy-...@googlegroups.com

I realized the call can be simplified to:

os.execlp('python', 'python', 'hello.py')

husamal...@gmail.com

unread,
Mar 12, 2022, 2:26:32 PM3/12/22
to Kivy users support
but the first app will stops when the second one start this is not my goal

also I noticed that the first file I shared works perfectly on android using pydroid

Elliot Garbus

unread,
Mar 12, 2022, 2:38:43 PM3/12/22
to kivy-...@googlegroups.com

At a high level, what is it you are trying to achieve?

husamal...@gmail.com

unread,
Mar 13, 2022, 5:11:53 AM3/13/22
to Kivy users support
I want to make a kivy compiler like an simple ide

husamal...@gmail.com

unread,
Mar 13, 2022, 5:13:40 AM3/13/22
to Kivy users support
simple app ide like run kivy code while the main app is still working

Elliot Garbus

unread,
Mar 13, 2022, 10:13:14 AM3/13/22
to kivy-...@googlegroups.com

If you want to create an interactive KV compiler, take a look at the “kivy catalog” in the examples directory.

 

You may want to look at the source code for IDLE. https://github.com/python/cpython/tree/main/Lib/idlelib

 

If you want a more full featured system that will handle python and KV, creating a new window while the existing window is open, I think you will need to use multi-processing.   https://docs.python.org/3/library/multiprocessing.html

 

There are some non obvious things to watch for when using multi-processing.  I documented a few learnings writing a multi-processing kivy app:

See: https://github.com/ElliotGarbus/MidiClockGenerator

This app uses MP to create a high resolution, low jitter timer in another process.  __name__ will get a different name in the process that is created by the program (__mp_main__). The test "if __name__ == '__main__':" prevents the kivy UI code from being loaded in both processes.

 

Subprocess maybe a simpler alternative to the Multi-processing library, that would work for your use case.  See: https://docs.python.org/3/library/subprocess.html

Reply all
Reply to author
Forward
0 new messages