Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Please help! App showing only black window!

29 views
Skip to first unread message

Busola Oronti

unread,
Apr 22, 2025, 1:47:56 PMApr 22
to Kivy users support
Hi everyone,

Please I need your help! I'm relatively new to kivy and KivyMD.

I'm trying to build an app that has two screens at the moment -  the Login and Home Screen. I got the code to run, but it only shows black screen.  Do you know what I'm doing wrong?

Below is my code:

import os

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.screen import MDScreen
#from kivymd.uix.appbar import MDActionBottomAppBarButton

# Simulated user credentials for authentication
user_credentials = {
    "admin": "admin123",
    "user": "user123",
    "test": "test123",
}

# Authentication function
def authenticate(username, password):
    return user_credentials.get(username) == password

KV = '''
#:import MDTopAppBar kivymd.uix.appbar.MDTopAppBar
#:import MDBottomAppBar kivymd.uix.appbar.MDBottomAppBar
#:import MDIconButton kivymd.uix.button.MDIconButton
#:import MDLabel kivymd.uix.label.MDLabel
#:import MDTextField kivymd.uix.textfield.MDTextField
#:import MDCard kivymd.uix.card.MDCard
#:import FitImage kivymd.uix.fitimage.FitImage
##:import MDActionBottomAppBarButton kivymd.uix.appbar.MDActionBottomAppBarButton

MDScreenManager:
    #id: screen_manager

    #LoginScreen:
    #HomeScreen:

<LoginScreen>:
    name: "login"
    #MDScreen:
    radius: [25, 25, 25, 25]
    md_bg_color: app.theme_cls.backgroundColor

    MDBoxLayout:
        orientation: "vertical"
        size_hint: 0.9, 0.9  # Essential for full-screen expansion
        spacing: "20dp"
        padding: "10dp"
        #md_bg_color: app.theme_cls.primaryColor

        MDCard:
            orientation: "vertical"
            adaptive_size: True
            theme_bg_color: "Custom"
            md_bg_color: self.theme_cls.backgroundColor
            padding: 10
            radius: [15]
           
            FitImage:
                source: "login_image2.png" if os.path.exists("login_image2.png") else ""
                pos_hint: {"top": 1}
                size_hint: 1, None
                fit_mode: "contain"
                height: "150dp"  # Explicit height for the image

        MDTextField:
            id: username_input
            hint_text: "Username"
            size_hint: 1, None
            height: "40dp"
            pos_hint: {"center_x": 0.5}

        MDTextField:
            id: password_input
            hint_text: "Password"
            password: True
            size_hint: 1, None
            height: "40dp"
            pos_hint: {"center_x": 0.5}
            icon_right: "eye-off-outline"
            icon_right_color: app.theme_cls.primary_color
            on_icon_right_release: app.toggle_password_visibility(self)

        Widget:
            size_hint_y: None
            height: "20dp"        
   
        MDButton:
            style: "elevated"
            theme_shadow_color: "Custom"
            shadow_color: "grey"
            pos_hint: {"center_x": .5, "center_y": .5}
            size_hint: 0.5, None
            height: "40dp"
            on_release:
                app.validate_user(
                username_input.text,
                password_input.text,
                )

                MDButtonText:
                    text: "Login"
                    font_style: "Title"

        MDLabel:
            id: result_label
            text: ""
            halign: "center"
            markup: True
            size_hint_y: None
            height: "30dp"

<HomeScreen>:
    name: "home"
   
    MDBoxLayout:
        orientation: "vertical"
       
        MDTopAppBar:
            title: "Home Screen"
            left_action_items: [["logout", lambda x: app.logout()]]
           
        MDLabel:
            text: "Welcome to the Home Screen!"
            halign: "center"
'''
class LoginScreen(MDScreen):
    pass

class HomeScreen(MDScreen):
    pass

class ACORNApp(MDApp):
    def file_exists(self, filename):
        return os.path.exists(filename)
   
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        self.theme_cls.theme_style = "Dark"
        #return Builder.load_string(KV)
        #return MDScreenManager()

        # Create screen manager
        self.sm = MDScreenManager()
        self.sm.add_widget(LoginScreen())
        self.sm.add_widget(HomeScreen())

        return self.sm
   
    def toggle_password_visibility(self, text_field):
        """
        Toggles the visibility of the password in the given MDTextField.
        """
        if text_field.password:
            text_field.password = False
            text_field.icon_right = "eye-outline"  # Change icon to show password
        else:
            text_field.password = True
            text_field.icon_right = "eye-off-outline"  # Change icon to hide password
   
   
    def validate_user(self, username, password):
        """
        Validates the username and password entered by the user.
        """
        if authenticate(username, password):
            self.sm.current = "home"  # Switch to the HomeScreen
        else:
            self.sm.get_screen("login").ids.result_label.text = "[color=#FF0000]Invalid username or password[/color]"

    def logout(self):
        """
        Logs the user out and switches back to the LoginScreen.
        """

        # Clear the login fields
        login_screen = self.sm.get_screen("login")
        login_screen.ids.username_input.text = ""
        login_screen.ids.password_input.text = ""
        login_screen.ids.result_label.text = ""
       
        self.sm.current = "login"  # Switch back to the LoginScreen


if __name__ == '__main__':
    ACORNApp().run()

Thanks!
Bussy

Busola Oronti

unread,
Apr 22, 2025, 2:20:09 PMApr 22
to kivy-...@googlegroups.com
I'm using Kivymd 2.0.1 dev 0 if this helps.

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/E5ZePIaEE_k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kivy-users/ffbe6c1a-84fc-478c-9fe2-ee782ed4df44n%40googlegroups.com.

ElliotG

unread,
Apr 22, 2025, 9:13:05 PMApr 22
to Kivy users support
You have defined the root widget, the screen manger in kv.  You also have the screens defined in kv, so you need to load the kv string.  I removed the code where you also created the screenmanager in python.   In the App class you can access the root widget, your screen manger, as self.root.  

I made a few other changes so you can run the code and move forward.  

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen


# Simulated user credentials for authentication
user_credentials = {
    "admin": "admin123",
    "user": "user123",
    "test": "test123",
}


# Authentication function

def authenticate(username, password):
    # return user_credentials.get(username) == password  # this is not correct...
    # assuming the username should be "user123" and the password should be "test123"
    return user_credentials.get('user') == username and user_credentials.get('test') == password

KV = '''
#:import os os

MDScreenManager:
    LoginScreen:
    HomeScreen:


<LoginScreen>:
    name: "login"
            icon_right_color: app.theme_cls.primary_palette
            # on_icon_right_release: app.toggle_password_visibility(self)
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        self.theme_cls.theme_style = "Dark"
        return Builder.load_string(KV)


    def toggle_password_visibility(self, text_field):
        """
        Toggles the visibility of the password in the given MDTextField.
        """
        if text_field.password:
            text_field.password = False
            text_field.icon_right = "eye-outline"  # Change icon to show password
        else:
            text_field.password = True
            text_field.icon_right = "eye-off-outline"  # Change icon to hide password

    def validate_user(self, username, password):
        """
        Validates the username and password entered by the user.
        """

        if authenticate(username, password):
            self.root.current = "home"  # Switch to the HomeScreen
        else:
            self.root.get_screen("login").ids.result_label.text = "[color=#FF0000]Invalid username or password[/color]"


    def logout(self):
        """
        Logs the user out and switches back to the LoginScreen.
        """
        # Clear the login fields
        login_screen = self.root.get_screen("login")

        login_screen.ids.username_input.text = ""
        login_screen.ids.password_input.text = ""
        login_screen.ids.result_label.text = ""
        self.root.current = "login"  # Switch back to the LoginScreen



if __name__ == '__main__':
    ACORNApp().run()
main.py

Amar kumar Sahu

unread,
Apr 23, 2025, 1:52:32 AMApr 23
to kivy-...@googlegroups.com

You can diagnose your code and check the logs and errors from AnvPy


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 visit https://groups.google.com/d/msgid/kivy-users/1b9f6757-96b6-4714-957b-f0742308f7c4n%40googlegroups.com.

Busola Oronti

unread,
Apr 23, 2025, 7:50:55 AMApr 23
to kivy-...@googlegroups.com
Thanks very much ElliotG. I'll try the code as edited and revert.

@Amar: thanks for the tool you suggested.



Reply all
Reply to author
Forward
0 new messages