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()