KivyMD icon indicating copy to clipboard operation
KivyMD copied to clipboard

Make the class names of the responsive screens (MobileScreenView, TabletScreenView, DesktopScreenView) unique while using kivymd.create_project or kivymd.add_view, to avoid kv resolution conflict for different parent screens

Open kengoon opened this issue 1 year ago • 15 comments

To clarify the issue mentioned in the title, when you create two different screens, such as LoginScreen and HomeScreen, and make them both responsive, they will each have three platforms available: MobileScreenView, TabletScreenView, and DesktopScreenView. However, Kivy can become confused because both screens have platforms with the same class name. Kivy will only use the first loaded KV class for any of the three platforms (MobileScreenView, TabletScreenView, or DesktopScreenView) and assign it to all the parent screens (such as LoginScreen and SignupScreen). This means that when you switch from LoginScreen to HomeScreen, you may see the same content on both screens.

I suggest a simple solution: to make each platform screen class unique to its parent screen. For example, platforms under LoginScreen should have class names prefixed with the first initials of their parent screen: LoginMobileScreenView, LoginTabletScreenView, and LoginDesktopScreenView. The same should apply to HomeScreen and its platforms.

kengoon avatar Mar 13 '23 08:03 kengoon

I don't see any problems since each screen is inherited from its own instance of the MDResponsiveLayout class.

HeaTTheatR avatar Mar 30 '23 06:03 HeaTTheatR

@HeaTTheatR The problem is in the kv file and not the instance. Ok just to give you a hint, if you do this <MDLabel> in your kv file does it create a new instance of MDLabel or does it override the existing MDLabel

kengoon avatar Mar 30 '23 11:03 kengoon

@kengoon I don't understand what you mean...

HeaTTheatR avatar Mar 30 '23 12:03 HeaTTheatR

@HeaTTheatR ok take for example, we have 2 screens one named HomeScreen and the other LoginScreen. both has responsive layouts and both has MobileScreenView class which allows you to make designs for mobile devices.

This is what the directory of HomeScreen and LoginScreen would look like:

LoginScreen/
    components/
        platforms/
            MobileScreen/
                mobile_screen.kv  <---------- <MobileScreenView> --------------->
                mobile_screen.py
                
                
HomeScreen/
    components/
        platforms/
            MobileScreen/
                mobile_screen.kv  <--------- <MobileScreenView> ---------------->
                mobile_screen.py
                

this <---------- <MobileScreenView> ---------------> means that the class MobileScreenView was defined in the kv.

Notice that kv file is defining the same class and kivy would assign the last loaded kv file with the class MobileScreenView to all different instance of MobileScreenView.

I don't know if I'm making sense to you. You can test this by creating 2 separate screens with responsive designs and make design on any or all of the responsive view (Mobile, Tablet and Desktop). Do this on both screens and try switching from one to the other, you will notice quickly that the screen remains the same, nothing changes

kengoon avatar Mar 30 '23 19:03 kengoon

You describe such actions that are unacceptable in development. I still don't see any problems.

HeaTTheatR avatar Apr 03 '23 12:04 HeaTTheatR

I don't think you are getting me, maybe I need to create a sample project and send it as a zip so you can test and see for your self

kengoon avatar Apr 03 '23 19:04 kengoon

@HeaTTheatR here is an example of what I was explaining. Look in the video below

https://user-images.githubusercontent.com/42192162/235224870-ae790d9e-51d2-4055-b78e-5f0aa9f7233c.mp4

notice the screen changes but using MVC with responsive screens, you will get this

https://user-images.githubusercontent.com/42192162/235225190-f224c65c-b130-4b24-84cf-21d3dd5d4630.mp4

notice that the screen does not change, and the screen it is showing is not the 1st screen but the last screen.

Example Test MVC project is in the zip file below, check the tablet_screen.kv in both FirstScreen and SecondScreen test_mvc.zip

kengoon avatar Apr 28 '23 18:04 kengoon

I don't understand what you want to explain to me...

HeaTTheatR avatar Apr 28 '23 18:04 HeaTTheatR

@HeaTTheatR run the program in the zip file. The Button on the FirstScreen is supposed to display "Kivy" But it is displaying "KivyMD" which is the button on the SecondScreen.

(edited) Note: Look into tablet_screen.kv on both FirstScreen and SecondScreen to see the difference in both code

kengoon avatar Apr 28 '23 18:04 kengoon

@HeaTTheatR if you can try to read this again keeping in mind what I just showed you above

To clarify the issue mentioned in the title, when you create two different screens, such as LoginScreen and HomeScreen, and make them both responsive, they will each have three platforms available: MobileScreenView, TabletScreenView, and DesktopScreenView. However, Kivy can become confused because both screens have platforms with the same class name. Kivy will only use the first loaded KV class for any of the three platforms (MobileScreenView, TabletScreenView, or DesktopScreenView) and assign it to all the parent screens (such as LoginScreen and SignupScreen). This means that when you switch from LoginScreen to HomeScreen, you may see the same content on both screens.

I suggest a simple solution: to make each platform screen class unique to its parent screen. For example, platforms under LoginScreen should have class names prefixed with the first initials of their parent screen: LoginMobileScreenView, LoginTabletScreenView, and LoginDesktopScreenView. The same should apply to HomeScreen and its platforms.

kengoon avatar Apr 28 '23 18:04 kengoon

@kengoon I figured out the reasons for this behavior. This is because KV files such as MobileScreenView, TabletScreenView and DesktopScreenView have the same names.

HeaTTheatR avatar Apr 29 '23 06:04 HeaTTheatR

@kengoon I figured out the reasons for this behavior. This is because KV files such as MobileScreenView, TabletScreenView and DesktopScreenView have the same names.

@HeaTTheatR 🫡🫡 Finally you understood me. Thanks for taking out the time to figure it out

kengoon avatar Apr 29 '23 06:04 kengoon

@kengoon Just need to use the prefix and the screen name, for example SecondScreenDesctopView

HeaTTheatR avatar Apr 29 '23 06:04 HeaTTheatR

And FirstScreenDesctopView

HeaTTheatR avatar Apr 29 '23 06:04 HeaTTheatR

@HeaTTheatR yes. This is way more simpler

kengoon avatar Apr 29 '23 06:04 kengoon