0% found this document useful (0 votes)
8 views10 pages

Document

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

Document

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

import json

import tkinter as tk

from tkinter import messagebox, simpledialog, scrolledtext, ttk

import logging

import os

# Configure logging

logging.basicConfig(filename='ewaste_management.log', level=logging.INFO,

format='%(asctime)s:%(levelname)s:%(message)s')

class EWaste:

def __init__(self, device_type, hazard_level):

self.device_type = device_type

self.hazard_level = hazard_level

self.recycling_options = []

def add_recycling_option(self, option):

self.recycling_options.append(option)

def to_dict(self):

return {

"device_type": self.device_type,

"hazard_level": self.hazard_level,

"recycling_options": self.recycling_options

}
def display_info(self):

info = f"Device Type: {self.device_type}\n" \

f"Hazard Level: {self.hazard_level}\n" \

f"Recycling Options: {', '.join(self.recycling_options)}"

return info

class Society:

def __init__(self):

self.e_waste_list = []

def add_e_waste(self, e_waste):

self.e_waste_list.append(e_waste)

def analyze_impact(self):

total_hazard = sum(device.hazard_level for device in self.e_waste_list)

if total_hazard > 50:

return "Warning: High e-waste hazard level! Urgent action required."

else:

return "E-waste hazard level is manageable."

def average_hazard_level(self):

if not self.e_waste_list:

return 0

return sum(device.hazard_level for device in self.e_waste_list) / len(self.e_waste_list)


def display_e_waste(self):

return "\n".join(device.display_info() for device in self.e_waste_list)

def save_to_file(self, filename):

with open(filename, 'w') as file:

json.dump([device.to_dict() for device in self.e_waste_list], file)

logging.info(f"E-waste data saved to {filename}")

def load_from_file(self, filename):

if not os.path.exists(filename):

logging.error(f"File {filename} not found.")

return f"File {filename} not found."

with open(filename, 'r') as file:

devices = json.load(file)

for device in devices:

ewaste = EWaste(device['device_type'], device['hazard_level'])

ewaste.recycling_options = device['recycling_options']

self.add_e_waste(ewaste)

logging.info(f"E-waste data loaded from {filename}")

class User:

def __init__(self, username, password):

self.username = username

self.password = password
class UserManager:

def __init__(self):

self.users = {}

self.load_users()

def load_users(self):

try:

with open('users.json', 'r') as file:

user_data = json.load(file)

for username, password in user_data.items():

self.users[username] = User(username, password)

except FileNotFoundError:

logging.warning("User file not found, starting with no users.")

def save_users(self):

with open('users.json', 'w') as file:

json.dump({username: user.password for username, user in self.users.items()}, file)

def register_user(self, username, password):

if username in self.users:

return False

self.users[username] = User(username, password)

self.save_users()

return True
def authenticate_user(self, username, password):

user = self.users.get(username)

return user is not None and user.password == password

class EWasteManagerApp:

def __init__(self, master):

self.master = master

self.master.title("E-Waste Management System")

self.society = Society()

self.user_manager = UserManager()

self.current_user = None

self.setup_ui()

def setup_ui(self):

self.login_frame = ttk.Frame(self.master)

self.login_frame.pack(pady=20)

self.label = tk.Label(self.login_frame, text="E-Waste Management System",


font=("Arial", 16))

self.label.grid(row=0, columnspan=2)

tk.Label(self.login_frame, text="Username:").grid(row=1, column=0)

self.username_entry = tk.Entry(self.login_frame)

self.username_entry.grid(row=1, column=1)
tk.Label(self.login_frame, text="Password:").grid(row=2, column=0)

self.password_entry = tk.Entry(self.login_frame, show='*')

self.password_entry.grid(row=2, column=1)

self.login_button = tk.Button(self.login_frame, text="Login", command=self.login)

self.login_button.grid(row=3, columnspan=2, pady=5)

self.register_button = tk.Button(self.login_frame, text="Register",


command=self.register)

self.register_button.grid(row=4, columnspan=2, pady=5)

def login(self):

username = self.username_entry.get()

password = self.password_entry.get()

if self.user_manager.authenticate_user(username, password):

self.current_user = username

logging.info(f"User {username} logged in.")

self.login_frame.pack_forget()

self.setup_management_ui()

else:

messagebox.showerror("Login Error", "Invalid username or password.")

def register(self):

username = self.username_entry.get()

password = self.password_entry.get()
if self.user_manager.register_user(username, password):

messagebox.showinfo("Success", "Registration successful!")

else:

messagebox.showerror("Error", "Username already exists.")

def setup_management_ui(self):

self.management_frame = ttk.Frame(self.master)

self.management_frame.pack(pady=20)

self.label = tk.Label(self.management_frame, text=f"Welcome, {self.current_user}!",


font=("Arial", 16))

self.label.pack(pady=10)

self.text_area = scrolledtext.ScrolledText(self.management_frame, width=60,


height=15, state='disabled')

self.text_area.pack(padx=10, pady=10)

self.add_button = tk.Button(self.management_frame, text="Add E-Waste",


command=self.add_e_waste)

self.add_button.pack(pady=5)

self.analyze_button = tk.Button(self.management_frame, text="Analyze Impact",


command=self.analyze_impact)

self.analyze_button.pack(pady=5)

self.display_button = tk.Button(self.management_frame, text="Display E-Waste",


command=self.display_e_waste)
self.display_button.pack(pady=5)

self.save_button = tk.Button(self.management_frame, text="Save Data",


command=self.save_data)

self.save_button.pack(pady=5)

self.load_button = tk.Button(self.management_frame, text="Load Data",


command=self.load_data)

self.load_button.pack(pady=5)

self.average_button = tk.Button(self.management_frame, text="Average Hazard Level",


command=self.average_hazard_level)

self.average_button.pack(pady=5)

def add_e_waste(self):

device_type = simpledialog.askstring("Input", "Enter device type (e.g., Smartphone,


Laptop):")

if not device_type:

return

hazard_level = simpledialog.askinteger("Input", "Enter hazard level (1-100):")

if not hazard_level or hazard_level < 1 or hazard_level > 100:

messagebox.showerror("Input Error", "Please enter a valid hazard level between 1


and 100.")

return

e_waste = EWaste(device_type, hazard_level)


while True:

option = simpledialog.askstring("Input", "Enter a recycling option (or 'done' to


finish):")

if option == 'done':

break

e_waste.add_recycling_option(option)

self.society.add_e_waste(e_waste)

messagebox.showinfo("Success", f"{device_type} added successfully.")

logging.info(f"{device_type} added by {self.current_user}.")

def analyze_impact(self):

result = self.society.analyze_impact()

messagebox.showinfo("Impact Analysis", result)

def display_e_waste(self):

self.text_area.config(state='normal')

self.text_area.delete(1.0, tk.END)

info = self.society.display_e_waste()

self.text_area.insert(tk.END, info)

self.text_area.config(state='disabled')

def save_data(self):

filename = simpledialog.askstring("Input", "Enter filename to save data:")

if filename:

self.society.save_to_file(filename)
messagebox.showinfo("Success", f"E-waste data saved to {filename}.")

def load_data(self):

filename = simpledialog.askstring("Input", "Enter filename to load data:")

if filename:

message

You might also like