Skip to content
Advertisement

I can’t switch screen in kivy

I followed a tutorial on youtube so I could learn how to use kivy, but when I try to switch screen it doesn’t work, I write my code the exact same way the guy does it, but for some reason nothing happens when I click the button “Se dine produkter”.

I have written where I need help in the .kv file, I added the rest of the code only in case if you need to know why I have done certain things. In line 10 and 25 I have written what I want to happen, I have tried so many different things and different ways to approach what I want, but I haven’t succeed with any of them.

main.py:

JavaScript

struktur.kv:

JavaScript

Advertisement

Answer

It happens because you are actually not using Screen Manager. Please follow official kivy documentation on how to use ScreenManager.

https://kivy.org/doc/stable/api-kivy.uix.screenmanager.html

Example code of build function using ScreenManager:

JavaScript

Example usage in .kv file:

JavaScript

There is some other ways to do it, but I don’t think your code is exactly the same as the code of the guy from YouTube. Stick to kivy documentation, it is actually good and you will learn ‘kivy ways’ of doing things.

Using switch_to(screen_name) not switch to(screen_name) won’t work either if you won’t use instance of your Screen Manager. Without that is just non existing function.

Here please you have your code fixed up a bit:

JavaScript

And kivy file:

JavaScript

Haven’t done everything but it displays screens and buttons do work. I have kicked out your class of ScreenManager and actually created an instance of it in the root of your app. That way you can call it from any screen, as they are added to the Manager. Like you need to give screens to the Manager before it can manage it. That way always works fine and you won’t experience any problems even if your app will contain 20 or more different classes with screens.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement