This code implements a simple appointment booking application using the...

August 25, 2025 at 04:05 PM

# main.py import sqlite3 from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.textinput import TextInput from kivy.uix.popup import Popup from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem from kivy.core.window import Window from kivy.uix.scrollview import ScrollView # Boje Window.clearcolor = (0, 0, 0, 1) ORANGE = (1, 0.5, 0, 1) WHITE = (1, 1, 1, 1) # Baza def init_db(): conn = sqlite3.connect("app.db") c = conn.cursor() c.execute("""CREATE TABLE IF NOT EXISTS users ( username TEXT PRIMARY KEY, password TEXT, is_admin INTEGER)""") # vode napravi admina c.execute("""CREATE TABLE IF NOT EXISTS appointments ( id INTEGER PRIMARY KEY AUTOINCREMENT, service TEXT, date TEXT, time TEXT, username TEXT)""") c.execute("SELECT * FROM users WHERE username='admin'") if not c.fetchone(): c.execute("INSERT INTO users VALUES (?, ?, ?)", ('admin', 'admin123', 1)) conn.commit() conn.close() def add_user(username, password, is_admin=0): conn = sqlite3.connect("app.db") c = conn.cursor() try: c.execute("INSERT INTO users VALUES (?, ?, ?)", (username, password, is_admin)) conn.commit() return True except sqlite3.IntegrityError: return False finally: conn.close() def check_user(username, password): conn = sqlite3.connect("app.db") c = conn.cursor() c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password)) user = c.fetchone() conn.close() return user def add_appointment(service, date, time, username): conn = sqlite3.connect("app.db") c = conn.cursor() c.execute("INSERT INTO appointments (service, date, time, username) VALUES (?, ?, ?, ?)", (service, date, time, username)) conn.commit() conn.close() def get_appointments(username=None): conn = sqlite3.connect("app.db") c = conn.cursor() if username: c.execute("SELECT service, date, time FROM appointments WHERE username=?", (username,)) else: c.execute("SELECT service, date, time, username FROM appointments") apps = c.fetchall() conn.close() return apps # Popup helper def show_popup(title, message): popup = Popup(title=title, content=Label(text=message, color=WHITE), size_hint=(0.7, 0.4), background_color=(0.1, 0.1, 0.1, 1)) popup.open() # Login screen tung tung tung sahur class LoginScreen(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) layout = BoxLayout(orientation='vertical', padding=20, spacing=15) title = Label(text="XWRAP STUDIO BiH", font_size=40, bold=True, color=ORANGE) self.username = TextInput(hint_text="Korisničko ime", multiline=False, size_hint_y=None, height=50) self.password = TextInput(hint_text="Lozinka", password=True, multiline=False, size_hint_y=None, height=50) login_btn = Button(text="Prijavi se", background_color=ORANGE, size_hint_y=None, height=50) signup_btn = Button(text="Registracija", background_color=ORANGE, size_hint_y=None, height=50) login_btn.bind(on_press=self.login) signup_btn.bind(on_press=self.signup) layout.add_widget(title) layout.add_widget(self.username) layout.add_widget(self.password) layout.add_widget(login_btn) layout.add_widget(signup_btn) self.add_widget(layout) def login(self, instance): uname = self.username.text.strip() pwd = self.password.text.strip() user = check_user(uname, pwd) if user: self.manager.current = 'admin' if user[2] == 1 else 'home' self.manager.get_screen('home').set_user(uname) else: show_popup("Greška", "Pogrešno korisničko ime ili lozinka!") def signup(self, instance): uname = self.username.text.strip() pwd = self.password.text.strip() if len(uname) < 3 or len(pwd) < 3: show_popup("Greška", "Unesite validne podatke!") elif add_user(uname, pwd): show_popup("Uspjeh", "Registracija uspješna!") else: show_popup("Greška", "Korisnik već postoji!") # Home screen class HomeScreen(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) self.current_user = None self.tabs = TabbedPanel(do_default_tab=False, tab_height=50, background_color=(0.1, 0.1, 0.1, 1)) #Zakaži self.book_tab = TabbedPanelItem(text="📅 Zakaži") self.book_layout = BoxLayout(orientation='vertical', padding=20, spacing=15) self.service = TextInput(hint_text="Usluga (npr. Full Wrap)", multiline=False, size_hint_y=None, height=50) self.date = TextInput(hint_text="Datum (dd.mm.yyyy)", multiline=False, size_hint_y=None, height=50) self.time = TextInput(hint_text="Vrijeme (hh:mm)", multiline=False, size_hint_y=None, height=50) book_btn = Button(text="Potvrdi", background_color=ORANGE, size_hint_y=None, height=50) book_btn.bind(on_press=self.book_appointment) self.book_layout.add_widget(self.service) self.book_layout.add_widget(self.date) self.book_layout.add_widget(self.time) self.book_layout.add_widget(book_btn) self.book_tab.add_widget(self.book_layout) #Termini self.apps_tab = TabbedPanelItem(text="🗂️ Termini") self.scroll = ScrollView() self.apps_layout = BoxLayout(orientation='vertical', size_hint_y=None) self.apps_layout.bind(minimum_height=self.apps_layout.setter('height')) self.scroll.add_widget(self.apps_layout) self.apps_tab.add_widget(self.scroll) #O nama about_tab = TabbedPanelItem(text="ℹ️ O nama") about_layout = BoxLayout(orientation='vertical', padding=20) about_layout.add_widget(Label( text="XWRAP STUDIO BiH\nWRAP IT YOU DESERVE IT\nInstagram: @xwrap_sarajevo\nFacebook: Xwrap Studio Sarajevo\nEmail: info@xwrapstudio.ba", color=WHITE, halign="center")) about_tab.add_widget(about_layout) self.tabs.add_widget(self.book_tab) self.tabs.add_widget(self.apps_tab) self.tabs.add_widget(about_tab) self.add_widget(self.tabs) def set_user(self, username): self.current_user = username self.refresh_appointments() def refresh_appointments(self): self.apps_layout.clear_widgets() for a in get_appointments(self.current_user): self.apps_layout.add_widget(Label( text=f"{a[0]} - {a[1]} {a[2]}", size_hint_y=None, height=40, color=WHITE)) def book_appointment(self, instance): add_appointment(self.service.text, self.date.text, self.time.text, self.current_user) show_popup("Uspjeh", "Termin uspješno zakazan!") self.refresh_appointments() # Admin class AdminScreen(Screen): def __init__(self, **kwargs): super().__init__(**kwargs) layout = BoxLayout(orientation='vertical', padding=20) layout.add_widget(Label(text="Admin Panel", font_size=30, color=ORANGE)) for a in get_appointments(): layout.add_widget(Label(text=f"{a[0]} - {a[1]} {a[2]} (Korisnik: {a[3]})", color=WHITE)) self.add_widget(layout) # Aplikacija patapim class MyApp(App): def build(self): self.title = "XWRAP STUDIO BiH" init_db() sm = ScreenManager(transition=FadeTransition()) sm.add_widget(LoginScreen(name='login')) sm.add_widget(HomeScreen(name='home')) sm.add_widget(AdminScreen(name='admin')) return sm if __name__ == '__main__': MyApp().run()

This code implements a simple appointment booking application using the Kivy framework for the user interface and SQLite for data storage. Here's what the code does in detail:

1. Database Setup and Functions

  • init_db(): Initializes an SQLite database (app.db) with two tables: users for storing user information (including an admin account by default) and appointments for storing appointment details. If the admin user doesn't already exist, it creates one with the username admin and password admin123.

  • add_user(username, password, is_admin=0): Adds a new user to the users table. Returns True if the user was added successfully; otherwise, it handles the integrity error (e.g., if the username is already taken) and returns False.

  • check_user(username, password): Checks if a user with the given username and password exists in the database. Returns the user record if it exists, or None otherwise.

  • add_appointment(service, date, time, username): Adds a new appointment associated with a specific username to the appointments table.

  • get_appointments(username=None): Retrieves appointments from the database. If a username is provided, it filters appointments for that user; otherwise, it retrieves all appointments.

  • show_popup(title, message): A helper function to show a message to the user using a small popup.

2. User Interface Screens

  • LoginScreen: Handles user login and registration.

    • Users can log in or sign up using a username and password.
    • After a successful login, users are redirected to either the admin or the home screen based on their user type (is_admin).
  • HomeScreen: The main screen for regular users.

    • "📅 Zakaži" Tab: Allows logged-in users to book an appointment by providing the service name, date, and time. Appointments are saved to the database.
    • "🗂️ Termini" Tab: Displays the logged-in user's appointments in a scrollable list.
    • "ℹ️ O nama" Tab: Displays some "about us" information hard-coded into the app.
  • AdminScreen: The admin view, which displays all appointments across users. It allows the admin to monitor bookings made by all users, but it does not provide any functionality for modifying or canceling the appointments.

3. Application Setup and Navigation

  • Kivy Components: The application uses several Kivy UI components such as ScreenManager, BoxLayout, Label, Button, TextInput, Popup, and TabbedPanel to design and transition between different screens in the application.

  • ScreenManager:

    • LoginScreen: The initial screen where users log in or register.
    • HomeScreen: Displayed for regular users after a successful login.
    • AdminScreen: Displayed for admin users after a successful login.
  • Transitions: The screen transitions are animated using a FadeTransition effect.

4. Application Workflow

  1. Initializes the app database (init_db()).
  2. Starts the application with the LoginScreen.
  3. Users can:
    • Log in with valid credentials. Depending on whether the user is an admin (is_admin=1), they are redirected to either the AdminScreen or HomeScreen.
    • Register a new account by providing a username and password.
  4. Regular users can:
    • Book new appointments through the "📅 Zakaži" tab.
    • View their own appointments in the "🗂️ Termini" tab.
  5. Admin users can:
    • View all appointments in the AdminScreen.

5. Visual Customizations

  • The background color of the window is set to black ((0, 0, 0, 1)).
  • The application uses colors like ORANGE ((1, 0.5, 0, 1)) and WHITE ((1, 1, 1, 1)).
  • Fonts are styled with bold text and larger sizes in key areas (e.g., heading).

Purpose and Functionality:

This application provides a basic system for booking and managing appointments for a studio. It supports:

  • User authentication (login and registration).
  • Appointment booking and viewing for regular users.
  • Admin-level access to view all user appointments.

This kind of app could be used in small businesses, such as studios or service-based companies, to schedule and manage appointments.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node