My problem is not very complex, because I recently started interfacing with kivy and by running the code in a tutorial (where the code worked) it generates this type of error that, looking on the internet and in the tutorial itself, I don't have found a solution.
main.py:
from kivy.app import App
class MainApp(App):
pass
MainApp().run()
main.kv:
#:include Screen1.kv
#:include Screen2.kv
ScreenManager:
Screen:
name: "main"
BoxLayout:
Button:
text: "go to Screen1"
on_release: app.root.current = "screen1"
Button:
text: "go to Screen2"
on_release: app.root.current = "screen2"
Screen1:
Screen2:
Screen1.kv:
<Screen1@Screen>:
name: "screen1"
BoxLayout:
Button:
text: "go to main"
on_release: app.root.current = "main"
Screen2.kv:
<Screen2@Screen>:
name: "screen2"
BoxLayout:
Button:
text: "go to main"
on_release: app.root.current = "main"
The error is this:
C:\Users\39377\AppData\Local\Microsoft\WindowsApps\python3.10.exe C:\Users\39377\Desktop\APP\BubiniProject\BubiniApp\main.py
[INFO ] [Logger ] Record log in C:\Users\39377\.kivy\logs\kivy_23-03-19_33.txt
[INFO ] [deps ] Successfully imported "kivy_deps.angle" 0.3.3
[INFO ] [deps ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO ] [deps ] Successfully imported "kivy_deps.sdl2" 0.4.5
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Users\39377\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 189 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.6.0 - Build 26.20.100.7870'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) UHD Graphics 620'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60 - Build 26.20.100.7870'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
Traceback (most recent call last):
File "C:\Users\39377\Desktop\APP\BubiniProject\BubiniApp\main.py", line 8, in <module>
MainApp().run()
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\app.py", line 954, in run
self._run_prepare()
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\app.py", line 923, in _run_prepare
self.load_kv(filename=self.kv_file)
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\app.py", line 696, in load_kv
root = Builder.load_file(rfilename)
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\lang\builder.py", line 305, in load_file
return self.load_string(data, **kwargs)
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\lang\builder.py", line 407, in load_string
self._apply_rule(
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\lang\builder.py", line 659, in _apply_rule
widget.add_widget(child)
File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\uix\screenmanager.py", line 987, in add_widget
raise ScreenManagerException(
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
Process finished with exit code 1
I don't understand why with such a simple program and writing the code copied from a tutorial (where in the video it is clearly seen that the code is successfully executed) this error is generated. Thanks in advance.
In your kv
, you are trying to add a BoxLayout
to the ScreenManager
, which causes that error. Perhaps you intended for the BoxLayout
to be added to the Screen
? Try changing:
ScreenManager:
Screen:
name: "main"
BoxLayout:
Button:
text: "go to Screen1"
on_release: app.root.current = "screen1"
Button:
text: "go to Screen2"
on_release: app.root.current = "screen2"
Screen1:
Screen2:
to:
ScreenManager:
Screen:
name: "main"
BoxLayout:
Button:
text: "go to Screen1"
on_release: app.root.current = "screen1"
Button:
text: "go to Screen2"
on_release: app.root.current = "screen2"
Screen1:
Screen2:
Note te indentation of the BoxLayout
.