0% found this document useful (0 votes)
60 views5 pages

From Import From Import From Import From Import, From Import Import

This document defines a Python application using Kivy and KivyMD for a login/registration screen interface with SQLite database. It includes classes for screen management and database functions for user validation and registration. The main app class initializes the database, defines screen switching based on login credentials, and includes placeholders for future lease management functions.

Uploaded by

Dylan Loty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

From Import From Import From Import From Import, From Import Import

This document defines a Python application using Kivy and KivyMD for a login/registration screen interface with SQLite database. It includes classes for screen management and database functions for user validation and registration. The main app class initializes the database, defines screen switching based on login credentials, and includes placeholders for future lease management functions.

Uploaded by

Dylan Loty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

from kivy.

lang import Builder


from kivy.uix.screenmanager import Screen

from kivymd.app import MDApp


from kivymd.uix.button import MDFlatButton, MDRaisedButton
from kivymd.uix.dialog import MDDialog

import sqlite3

KV = '''
ScreenManager:
LoginScreen:
RegisterScreen:
AdminScreen:
UserScreen:

<LoginScreen>:
name: 'login'
MDTextField:
id: username
hint_text: 'Username'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
size_hint_x: None
width: 300
MDTextField:
id: password
hint_text: 'Password'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint_x: None
width: 300
password: True
MDRaisedButton:
text: 'Login'
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
on_release: app.login(username.text, password.text)
MDFlatButton:
text: 'Register'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_release: root.manager.current = 'register'

<RegisterScreen>:
name: 'register'
MDTextField:
id: reg_username
hint_text: 'Username'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
size_hint_x: None
width: 300
MDTextField:
id: reg_password
hint_text: 'Password'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint_x: None
width: 300
password: True
MDRaisedButton:
text: 'Register'
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
on_release: app.register(reg_username.text, reg_password.text)
MDFlatButton:
text: 'Back to Login'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_release: root.manager.current = 'login'

<AdminScreen>:
name: 'admin'
MDLabel:
text: 'Welcome, Admin!'
halign: 'center'
MDFlatButton:
text: 'Logout'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_release: root.manager.current = 'login'

<UserScreen>:
name: 'user'
MDLabel:
id: user_label
text: ''
halign: 'center'
MDFlatButton:
text: 'Logout'
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_release: root.manager.current = 'login'
'''

class LoginScreen(Screen):
pass

class RegisterScreen(Screen):
pass

class AdminScreen(Screen):
pass
class UserScreen(Screen):
pass

class LeaseApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.init_database()

def build(self):
self.theme_cls.primary_palette = "Blue"
self.theme_cls.primary_hue = "500"
self.theme_cls.theme_style = "Light"
return Builder.load_string(KV)

def init_database(self):
conn = sqlite3.connect('users.db')
cursor = conn.cursor()

cursor.execute('''CREATE TABLE IF NOT EXISTS users


(username TEXT PRIMARY KEY NOT NULL, password TEXT
NOT NULL);''')
conn.commit()
conn.close()

def login(self, username, password):


if username == 'admin' and password == 'admin':
self.root.current = 'admin'
elif self.validate_user(username, password):
self.root.get_screen('user').ids.user_label.text = f"Welcome,
{username}!"
self.root.current = 'user'
else:
self.show_alert_dialog("Invalid username or password")

def register(self, username, password):


conn = sqlite3.connect('users.db')
cursor = conn.cursor()

try:
cursor.execute("INSERT INTO users (username, password) VALUES
(?, ?)", (username, password))
conn.commit()
self.show_alert_dialog("Registration successful!")
self.root.current = 'login'
except sqlite3.IntegrityError:
self.show_alert_dialog("Username already exists")
finally:
conn.close()

def validate_user(self, username, password):


conn = sqlite3.connect('users.db')
cursor = conn.cursor()

cursor.execute("SELECT * FROM users WHERE username = ? AND password


= ?", (username, password))
user = cursor.fetchone()
conn.close()

if user:
return True
else:
return False

def show_alert_dialog(self, message):


dialog = MDDialog(
title="Notice",
text=message,
size_hint=(0.8, 1),
buttons=[
MDFlatButton(
text="OK", on_release=lambda x: dialog.dismiss()
),
],
)
dialog.open()

def create_novated_lease(self):
# Add your code for creating a new novated lease
pass

def check_previous_leases(self):
# Add your code for checking previous leases
pass

def show_help_dialog(self):
help_text = "This is a help message. Add more information here."
dialog = MDDialog(
title="Help",
text=help_text,
size_hint=(0.8, 1),
buttons=[
MDFlatButton(
text="OK", on_release=lambda x: dialog
),
],
)
dialog.open()

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

You might also like