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

Psswrdmnger 2

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)
16 views5 pages

Psswrdmnger 2

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 customtkinter import *

Import mysql.connector

Class DatabaseMediator:

“””Handles the connection and interaction with the MySQL database.”””

Def __init__(self, host, user, password, database, table_name,*columns):

“””

Initializes the DatabaseMediator object.

Args:

Host (str): The MySQL server host.

User (str): The username for the database connection.

Password (str): The password for the database connection.

Database (str): The name of the database.

Table_name (str): The name of the table storing the passwords.

*columns (str): Variable-length argument list of column names in the


table.

“””

Try:

Self.host = host

Self.user = user

Self.password = password

Self.database = database

Self.table_name = table_name

Self.columns = columns

Self.mydb = mysql.connector.connect(

Host=self.host,
User=self.user,

Password=self.password,

Database=self.database

Self.mycursor = self.mydb.cursor()

Except Exception:

Print(“Cannot connect to the database!”)

Def insert_data(self, *args):

“””

Inserts data into the MySQL table.

Args:

*args: Variable-length argument list of values to be inserted.

“””

Try:

Self.mycursor.execute(f”INSERT INTO {self.table_name} ({‘,


‘.join(list(self.columns))}) VALUES {args}”)

Self.mydb.commit()

Except Exception:

Print(“These values can’t be inserted!!”)

Def retrieve_data(self):

“””

Retrieves all data from the MySQL table.

Returns:
List: A list of tuples representing the retrieved data.

“””

Try:

Self.mycursor.execute(f”SELECT * FROM {self.table_name}”)

Myresult = self.mycursor.fetchall()

Return myresult

Except Exception:

Print(“Couldn’t retrieve data!”)

Class PasswordManager(CTk):

“””The main GUI class for the Password Manager application.”””

Def __init__(self):

“””Initializes the PasswordManager GUI.”””

Super().__init__()

Self.title(“Password Manager”)

Self.database_connection = DatabaseMediator(“localhost”, “root”,


“maitry”, “password_manager”, “passwords”, “account_name”,
“account_id”, “account_password”)

Self.frame = CTkFrame(self)

Self.frame.grid(row=0, columnspan=2, padx=10, pady=10)

Self.account_name = CTkEntry(self.frame, placeholder_text=”Account


name”)

Self.account_name.pack(padx=10, pady=10)
Self.account_id = CTkEntry(self.frame, placeholder_text=”Enter your
user ID here”)

Self.account_id.pack(padx=10, pady=10)

Self.account_password = CTkEntry(self.frame, placeholder_text=”Enter


your Password”)

Self.account_password.pack(padx=10, pady=10)

Self.save_password = CTkButton(self, text=”Save Password”,


command=self.save)

Self.save_password.grid(row=1, column=0, padx=10, pady=10)

Self.view_password = CTkButton(self, text=”View Passwords”,


command=self.view)

Self.view_password.grid(row=1, column=1, padx=10, pady=10)

Def save(self):

“””Saves the password to the database.”””

Acc_nm = self.account_name.get()

Acc_id = self.account_id.get()

Acc_password = self.account_password.get()

Self.database_connection.insert_data(acc_nm, acc_id, acc_password)

Label = CTkLabel(self, text=”Passwords Saved successfully…”)

Label.grid(row=2, columnspan=2, padx=10, pady=10)

Def view(self):

“””Retrieves and displays all passwords from the database.”””

Passwords = self.database_connection.retrieve_data()
Result = “”

For I in range(len(passwords)):

Result += f”\n{i+1}. {passwords[i]}”

Result_frame = CTkFrame(self)

Result_frame.grid(row=3, columnspan=2, padx=10, pady=10)

Res_label = CTkLabel(result_frame, text=result)

Res_label.pack()

App = PasswordManager()

App.mainloop()

You might also like