Untitled

 avatar
user_3827584
python
2 years ago
13 kB
1
Indexable
Never
from tkinter import *
from tkinter import messagebox
import os

# Constants
WINDOW_PAD = 35


# Register and Login Screen (Main Screen)


class Main:
    def __init__(self):
        self.window = Tk()
        self.window.title("Your Pocket")
        self.window.resizable(width=False, height=False)
        self.window.config(padx=WINDOW_PAD, pady=WINDOW_PAD)

        # Variables
        self.temp_name = StringVar()
        self.temp_username = StringVar()
        self.temp_age = StringVar()
        self.temp_gender = StringVar()
        self.temp_password = StringVar()
        self.temp_balance = StringVar()
        self.temp_login_username = StringVar()
        self.temp_login_password = StringVar()
        self.register_notif = None
        self.login_notif = None
        self.login_screen = None

        # Menubar
        self.create_menubar()

        # Titles
        self.text_frame = self.create_frame()
        self.create_title("Your Pocket", ("Calibre", 14))
        self.create_title("the money management system", ("Calibre", 12))

        # Buttons
        self.button_frame = self.create_frame(10, 10)
        self.create_register_button()
        self.create_login_button()

        self.window.mainloop()

    # Create Widget for main
    def create_menubar(self):
        menubar = Menu(self.window, relief=FLAT, bd=0)

        window_menu = Menu(menubar, tearoff=0)
        window_menu.add_command(label="Register", command=self.register)
        window_menu.add_separator()
        window_menu.add_command(label="Login", command=self.login)

        quit_menu = Menu(menubar, tearoff=0)
        quit_menu.add_command(label="Quit", command=self.window.quit)

        menubar.add_cascade(label="Window", menu=window_menu)
        menubar.add_cascade(label="Exit", menu=quit_menu)

        self.window.config(menu=menubar)

    def create_frame(self, x_input=0, y_input=0):
        frame = Frame(self.window)
        frame.pack(padx=x_input, pady=y_input, expand=True)
        return frame

    def create_title(self, text_input, font_input):
        label = Label(self.text_frame, text=text_input, font=font_input)
        label.pack(expand=True)

    def create_register_button(self):
        button = Button(self.button_frame, text="Register", font=("Calibre", 12), width=20, command=self.register)
        button.pack(expand=True)

    def create_login_button(self):
        button = Button(self.button_frame, text="Login", font=("Calibre", 12), width=20, command=self.login)
        button.pack(expand=True)

    # Functions
    def finish_reg(self):
        name = self.temp_name.get()
        user_name = self.temp_username.get()
        age = self.temp_age.get()
        gender = self.temp_gender.get()
        password = self.temp_password.get()
        balance = self.temp_balance.get()
        all_accounts = os.listdir()

        def show_status():
            self.register_notif.grid(row=6, column=0, columnspan=2, sticky="N", pady=1)

        if name == "" or user_name == "" or age == "" or gender == "" or password == "" or balance == "":
            self.register_notif.config(fg="red", text="All fields required*")
            show_status()
            return

        if user_name in all_accounts:
            self.register_notif.config(fg="red", text="Account already exits")
            show_status()
        else:
            with open(user_name, "w") as new_file:
                new_file.write(name + "\n")
                new_file.write(user_name + "\n")
                new_file.write(password + "\n")
                new_file.write(age + "\n")
                new_file.write(gender + "\n")
                new_file.write(balance + "\n")
                new_file.write("0")
            self.register_notif.config(fg="green", text="Account has been created")
            show_status()

    def login_session(self):
        all_accounts = os.listdir()
        login_username = self.temp_login_username.get()
        login_password = self.temp_login_password.get()

        def show_status():
            self.login_notif.grid(row=4, column=0, columnspan=2, sticky="N", pady=1)

        for username in all_accounts:
            if username == login_username:
                with open(username) as file:
                    file_data = file.read()
                    file_data = file_data.split("\n")
                    password = file_data[2]

                    # Account Dashboard
                    if login_password == password:
                        self.login_screen.destroy()
                        self.window.destroy()
                        Dashboard(file_data[0], username, file_data[3], file_data[4], file_data[5])
                        return
                    else:
                        self.login_notif.config(fg="red", text="Password incorrect")
                        show_status()
                        return

        self.login_notif.config(fg="red", text="Account not found")
        show_status()

    def register(self):
        # Register Screen
        register_screen = Toplevel(self.window)
        register_screen.title("Register")
        register_screen.resizable(width=False, height=False)
        register_screen.config(padx=WINDOW_PAD, pady=WINDOW_PAD)

        # Frames
        register_middle = Frame(register_screen)
        register_middle.grid(row=1, column=0, padx=5, pady=20)

        # Labels
        Label(register_screen, text="Enter these following fields*", font=("Calibre", 12)) \
            .grid(row=0, column=0, columnspan=2, sticky=N)
        Label(register_middle, text="Name", font=("Calibre", 12)).grid(row=1, column=0, sticky=E, padx=5)
        Label(register_middle, text="Username", font=("Calibre", 12)).grid(row=2, column=0, sticky=E, padx=5)
        Label(register_middle, text="Age", font=("Calibre", 12)).grid(row=3, column=0, sticky=E, padx=5)
        Label(register_middle, text="Gender", font=("Calibre", 12)).grid(row=4, column=0, sticky=E, padx=5)
        Label(register_middle, text="Password", font=("Calibre", 12)).grid(row=5, column=0, sticky=E, padx=5)
        Label(register_middle, text="Current balance", font=("Calibre", 12)).grid(row=6, column=0, sticky=E, padx=5)
        self.register_notif = Label(register_screen, font=("Calibre", 12))
        self.register_notif.grid_remove()

        # Entries
        Entry(register_middle, textvariable=self.temp_name).grid(row=1, column=1, padx=5)
        Entry(register_middle, textvariable=self.temp_username).grid(row=2, column=1, padx=5)
        Entry(register_middle, textvariable=self.temp_age).grid(row=3, column=1, padx=5)
        Entry(register_middle, textvariable=self.temp_gender).grid(row=4, column=1, padx=5)
        Entry(register_middle, textvariable=self.temp_password, show="*").grid(row=5, column=1, padx=5)
        Entry(register_middle, textvariable=self.temp_balance).grid(row=6, column=1, padx=5)

        # Button
        register_enter = Button(register_screen, text="Register", command=self.finish_reg, font=("Calibre", 12))
        register_enter.grid(row=5, pady=10, columnspan=2)

    def login(self):
        # Login Screen
        self.login_screen = Toplevel(self.window)
        self.login_screen.title("Login")
        self.login_screen.resizable(width=False, height=False)
        self.login_screen.config(padx=WINDOW_PAD, pady=WINDOW_PAD)

        # Frames
        login_middle = Frame(self.login_screen)
        login_middle.grid(row=1, column=0, padx=5, pady=20)

        # Labels
        Label(self.login_screen, text="Login to your account", font=("Calibre", 12)) \
            .grid(row=0, column=0, columnspan=2, sticky=N)
        Label(login_middle, text="Username", font=("Calibre", 12)).grid(row=1, column=0, sticky=E)
        Label(login_middle, text="Password", font=("Calibre", 12)).grid(row=2, column=0, sticky=E)
        self.login_notif = Label(self.login_screen, font=("Calibre", 12))
        self.login_notif.grid_remove()

        # Entries
        Entry(login_middle, textvariable=self.temp_login_username).grid(row=1, column=1, padx=5)
        Entry(login_middle, textvariable=self.temp_login_password, show="*").grid(row=2, column=1, padx=5)

        # Button
        login_enter = Button(self.login_screen, text="Login", command=self.login_session, font=("Calibre", 12))
        login_enter.grid(row=3, columnspan=2)


# Dashboard

class Dashboard:
    def __init__(self, name, user_name, age, gender, balance):
        self.window = Tk()
        self.window.title("Your Pocket")
        self.window.resizable(width=False, height=False)
        self.window.config(padx=WINDOW_PAD, pady=WINDOW_PAD)
        self.window.geometry("650x450")

        # Variables
        self.name = name
        self.user_name = user_name
        self.age = age
        self.gender = gender
        self.balance = balance

        self.create_menubar()

        # Titles
        self.text_frame = self.create_top_frame()
        self.create_title("Welcome, " + self.user_name, ("Calibre", 14), 0, 0)
        self.create_title("to Your Pocket ", ("Calibre", 12), 1, 0)

        # SEP
        self.create_start_separate()

        # Details and Functions
        self.details_frame = self.create_left_frame()
        self.create_details()

        self.pocket_function_frame = self.create_right_frame()
        self.create_pocket_functions()

        self.window.mainloop()

    # Create Widget for main

    # Menubar
    def create_menubar(self):
        menubar = Menu(self.window, relief=FLAT, bd=0)

        quit_menu = Menu(menubar, tearoff=0)
        quit_menu.add_command(label="Quit", command=self.window.quit)

        menubar.add_cascade(label="Exit", menu=quit_menu)

        self.window.config(menu=menubar)

    # Frames
    def create_top_frame(self):
        frame = Frame(self.window)
        frame.pack(side="top")
        return frame

    def create_left_frame(self):
        frame = Frame(self.window)
        frame.pack(side="left", expand=True)
        return frame

    def create_right_frame(self):
        frame = Frame(self.window)
        frame.pack(side="right", expand=True)
        return frame

    # Title
    def create_title(self, text_input, font_input, row_input, column_input):
        label = Label(self.text_frame, text=text_input, font=font_input)
        label.grid(row=row_input, column=column_input)
        return label

    def create_start_separate(self):
        separate = Label(self.text_frame, text="=================================================================",
                         font=("Calibre", 8))
        separate.grid(row=2, column=0, pady=10)

    # Details
    def create_details(self):
        Label(self.details_frame, text="Current balance: " + self.balance, font=("Calibre", 12, "bold")) \
            .grid(row=0, column=0, sticky=E)
        Label(self.details_frame, text="Name: " + self.name, font=("Calibre", 12)) \
            .grid(row=1, column=0, sticky=E)
        Label(self.details_frame, text="Username: " + self.user_name, font=("Calibre", 12)) \
            .grid(row=2, column=0, sticky=E)
        Label(self.details_frame, text="Age: " + self.age, font=("Calibre", 12)) \
            .grid(row=3, column=0, sticky=E)
        Label(self.details_frame, text="Status: " + ("Can access all" if int(self.age) >= 18 else "Some feature may "
                                                                                                  "unavailable"),
              font=("Calibre", 12)).grid(row=4, column=0, sticky=E)
        Label(self.details_frame, text="Gender: " + self.gender, font=("Calibre", 12)) \
            .grid(row=5, column=0, sticky=E)

    def create_pocket_functions(self):
        Label(self.pocket_function_frame, text="Pocket Features", font=("Calibre", 12, "bold")) \
            .grid(row=0, column=0, sticky=W, pady=10)
        Button(self.pocket_function_frame, text="Deposit / Withdrawal", font=("Calibre", 12), width=20) \
            .grid(row=1, column=0, sticky=W)
        Button(self.pocket_function_frame, text="Expenses Tracker", font=("Calibre", 12), width=20) \
            .grid(row=2, column=0, sticky=W, pady=10)
        Button(self.pocket_function_frame, text="Currency Converter", font=("Calibre", 12), width=20) \
            .grid(row=3, column=0, sticky=W)
        Button(self.pocket_function_frame, text="Savings", font=("Calibre", 12), width=20) \
            .grid(row=4, column=0, sticky=W, pady=10)
        Button(self.pocket_function_frame, text="Delete Account", font=("Calibre", 12), width=20) \
            .grid(row=5, column=0, sticky=W)


Main()