import tkinter as tk
from tkinter import messagebox
import csv
import random
import string
import os
def generate_rr_number():
"""Generates a unique RR number using random letters and numbers."""
return ''.join([Link](string.ascii_uppercase + [Link], k=8))
# Function for the first page
def first_page():
root = [Link]()
[Link]("Electricity Bill Generator")
[Link]("600x400")
[Link](bg="light pink")
# Title for the first page
title_label = [Link](root, text="Electricity Bill Generator", font=("Times New Roman", 24, "bold"),
bg="light pink")
title_label.pack(pady=20)
# Buttons for navigation
register_button = [Link](root, text="REGISTER", font=("Times New Roman", 24), bg="white",
command=register_page)
register_button.pack(pady=10)
login_button = [Link](root, text="LOGIN", font=("Times New Roman", 24), bg="white",
command=login_page)
login_button.pack(pady=10)
rate_button = [Link](root, text="CURRENT ELECTRICITY RATE", font=("Times New Roman", 24),
bg="white", command=check_rate_page)
rate_button.pack(pady=10)
admin_button = [Link](root, text="ADMIN LOGIN", font=("Times New Roman", 24), bg="white",
command=admin_login_page)
admin_button.pack(pady=10)
close_button = [Link](root, text="CLOSE APPLICATION", font=("Times New Roman", 24),
bg="white", command=[Link])
close_button.pack(pady=25)
[Link]()
# Function for the "Check Current Electricity Rate" page
def check_rate_page():
rate_window = [Link]()
rate_window.title("Current Electricity Rates")
rate_window.geometry("600x400")
rate_window.configure(bg="light pink")
title_label = [Link](rate_window, text="Electricity Rates", font=("Times New Roman", 24, "bold"),
bg="light pink")
title_label.grid(row=0, column=0, columnspan=2, pady=10)
# Updated electricity rate table with only two columns
table_data = [
["Fixed Charges", ""],
["For the first KW", "Rs.40.00"],
["For every additional KW", "Rs.50.00"],
["Variable Charges", ""],
["Slab 1 (0-30 Units)", "Rs.3.25/Unit"],
["Slab 2 (31-100 Units)", "Rs.4.70/Unit"],
["Slab 3 (101-200 Units)", "Rs.6.25/Unit"],
["Slab 4 (201-300 Units)", "Rs.7.30/Unit"],
["Slab 5 (301-400 Units)", "Rs.7.35/Unit"],
["Slab 6 (Above 400 Units)", "Rs.7.40/Unit"]
for row_index, row in enumerate(table_data):
for col_index, cell in enumerate(row):
label = [Link](rate_window, text=cell, font=("Times New Roman", 16), bg="light pink",
borderwidth=1, relief="solid", padx=10, pady=5)
[Link](row=row_index + 1, column=col_index, sticky="nsew")
back_button = [Link](rate_window, text="Back", font=("Times New Roman", 16), bg="white",
command=rate_window.destroy)
back_button.grid(row=len(table_data) + 2, column=0, columnspan=2, pady=10)
for col in range(2):
rate_window.grid_columnconfigure(col, weight=1)
# Register Page
def register_page():
global entries, connection_type, tariff_type, register_window
register_window = [Link]()
register_window.title("REGISTER")
register_window.geometry("600x600")
register_window.configure(bg="light pink")
title_label = [Link](register_window, text="REGISTER", font=("Times New Roman", 24, "bold"),
bg="light pink")
title_label.grid(row=0, column=0, columnspan=2, pady=20)
labels = [
"Full Name", "Date of Birth", "Mobile Number", "Email Address",
"Username", "Password", "Confirm Password", "House Number/Building Name",
"Area/Street", "Country", "State", "Pin Code"
entries = {}
for i, label_text in enumerate(labels):
label = [Link](register_window, text=label_text, font=("Times New Roman", 16), bg="light
pink")
[Link](row=i+1, column=0, padx=10, pady=5, sticky="w")
entry = [Link](register_window, font=("Times New Roman", 16))
[Link](row=i+1, column=1, padx=10, pady=5, sticky="ew") # Align entry to the width
entries[label_text] = entry
# Center-aligned dropdown for Connection Type
connection_label = [Link](register_window, text="Connection Type", font=("Times New Roman",
16), bg="light pink")
connection_label.grid(row=13, column=0, padx=10, pady=5, sticky="w")
connection_type = [Link](register_window)
connection_dropdown = [Link](register_window, connection_type, "RESIDENTIAL",
"COMMERCIAL", "INDUSTRIAL")
connection_dropdown.config(font=("Times New Roman", 16))
connection_dropdown.grid(row=13, column=1, padx=10, pady=5, sticky="ew")
# Center-aligned dropdown for Tariff Type
tariff_label = [Link](register_window, text="Tariff Type", font=("Times New Roman", 16),
bg="light pink")
tariff_label.grid(row=14, column=0, padx=10, pady=5, sticky="w")
tariff_type = [Link](register_window)
tariff_dropdown = [Link](register_window, tariff_type, "URBAN", "RURAL")
tariff_dropdown.config(font=("Times New Roman", 16))
tariff_dropdown.grid(row=14, column=1, padx=10, pady=5, sticky="ew")
submit_button = [Link](register_window, text="SUBMIT", font=("Times New Roman", 16),
bg="white", command=submit_registration)
submit_button.grid(row=15, column=0, columnspan=2, pady=20)
for col in range(2):
register_window.grid_columnconfigure(col, weight=1) # Center align columns
# Registration Success Popup Function
def registration_success_popup():
popup = [Link]()
[Link]("Registration Successful")
[Link]("400x200")
[Link](bg="light pink")
# Success message label
success_label = [Link](popup, text="REGISTRATION SUCCESSFUL.", font=("Times New Roman",
18, "bold"), bg="light pink")
success_label.pack(pady=20)
# Define the go_to_login function
def go_to_login():
[Link]()
first_page()
# Go back button to redirect to the login page
go_back_button = [Link](popup, text="GO BACK TO LOGIN PAGE", font=("Times New Roman",
14), bg="white", command=go_to_login)
go_back_button.pack(pady=10)
# Submit Registration Function
def submit_registration():
# Check if all fields are filled
for field, entry in [Link]():
if not [Link]():
[Link]("Error", f"Please enter {field}")
return
# Check if passwords match
if entries["Password"].get() != entries["Confirm Password"].get():
[Link]("Error", "Passwords do not match")
return
# Generate a unique RR Number
rr_number = generate_rr_number()
# Collect user data, including the RR Number
user_data = [
entries["Full Name"].get(),
entries["Date of Birth"].get(),
entries["Mobile Number"].get(),
entries["Email Address"].get(),
entries["Username"].get(),
entries["Password"].get(),
entries["House Number/Building Name"].get(),
entries["Area/Street"].get(),
entries["Country"].get(),
entries["State"].get(),
entries["Pin Code"].get(),
connection_type.get(),
tariff_type.get(),
rr_number
# Write data to CSV
with open("[Link]", mode="a", newline='') as file:
writer = [Link](file)
[Link](user_data)
registration_success_popup()
# LOGIN PAGE FUNCTION (With corrections)
def login_page():
login_window = [Link]()
login_window.title("Login Page")
login_window.geometry("600x400")
login_window.configure(bg="light pink")
# Login page title
title_label = [Link](login_window, text="Login Page", font=("Times New Roman", 32, "bold"),
bg="light pink")
title_label.pack(pady=20)
# Username and Password fields
username_label = [Link](login_window, text="Username", font=("Times New Roman", 32),
bg="light pink")
username_label.pack(pady=5)
username_entry = [Link](login_window, font=("Times New Roman",32))
username_entry.pack(pady=5)
password_label = [Link](login_window, text="Password", font=("Times New Roman",32),
bg="light pink")
password_label.pack(pady=5)
password_entry = [Link](login_window, show="*", font=("Times New Roman", 32))
password_entry.pack(pady=5)
# Validate Login Button
login_button = [Link](login_window, text="Login", font=("Times New Roman", 32), bg="white",
command=lambda: validate_login(username_entry.get(), password_entry.get(), login_window))
login_button.pack(pady=20)
login_window.mainloop()
# Validate Login Function
def validate_login(username, password, login_window):
with open("[Link]", mode="r") as file:
reader = [Link](file)
for row in reader:
if row[4] == username and row[5] == password:
user_full_name = row[0]
rr_number = row[-1] # Assuming RR number is the last element
[Link]("Success", "Login successful!")
login_window.destroy()
user_page(user_full_name, rr_number, username) # Pass username here
return
[Link]("Error", "Invalid username or password")
# USER PAGE FUNCTION
def user_page(user_full_name, rr_number, username):
user_window = [Link]()
user_window.title("User Page")
user_window.geometry("600x400")
user_window.configure(bg="light pink")
welcome_label = [Link](user_window, text=f"WELCOME, {user_full_name}", font=("Times New
Roman", 25), bg="light pink")
welcome_label.pack(pady=25)
# Buttons to navigate to other pages
view_bills_button = [Link](user_window, text="VIEW YOUR BILLS", font=("Times New Roman",
24), bg="white", command= view_bills)
view_bills_button.pack(pady=25)
view_profile_button = [Link](user_window, text="VIEW PROFILE", font=("Times New Roman",
24), bg="white", command=lambda: view_profile_page(user_full_name, username))
view_profile_button.pack(pady=25)
edit_button = [Link](user_window, text="EDIT ACCOUNT DETAILS", font=("Times New Roman",
24), bg="white", command=lambda: edit_account_details(username))
edit_button.pack(pady=25)
change_password_button = [Link](user_window, text="CHANGE YOUR PASSWORD",
font=("Times New Roman", 24), bg="white", command=change_password_page)
change_password_button.pack(pady=25)
delete_account_button = [Link](user_window, text="DELETE ACCOUNT", font=("Times New
Roman", 24), bg="white", command=lambda: delete_account_page(username))
delete_account_button.pack(pady=25)
logout_button = [Link](user_window, text="LOGOUT", font=("Times New Roman", 24),
bg="white", command=user_window.destroy)
logout_button.pack(pady=25)
user_window.mainloop()
# View Profile Page Function
def view_profile_page(user_full_name, username):
profile_window = [Link]()
profile_window.title("View Profile")
profile_window.geometry("600x600")
profile_window.configure(bg="light pink")
# Profile Title
title_label = [Link](profile_window, text="User Profile", font=("Times New Roman", 24, "bold"),
bg="light pink")
title_label.pack(pady=20)
# Retrieve User Details from CSV
try:
with open("[Link]", mode="r") as file:
reader = [Link](file)
for row in reader:
if row[4] == username: # Match by username
profile_data = {
"Full Name": row[0],
"Date of Birth": row[1],
"Mobile Number": row[2],
"Email Address": row[3],
"Username": row[4],
"Password": row[5],
"Confirm Password": row[5], # Passwords are the same in this app
"House Number/Building Name": row[6],
"Area/Street": row[7],
"Country": row[8],
"State": row[9],
"Pin Code": row[10],
"Connection Type": row[11],
"Tariff Type": row[12],
"RR Number": row[13]
break
else:
[Link]("Error", "User details not found!")
profile_window.destroy()
return
except FileNotFoundError:
[Link]("Error", "User data file not found!")
profile_window.destroy()
return
# Display User Details
for key, value in profile_data.items():
frame = [Link](profile_window, bg="light pink")
[Link](fill="x", padx=20, pady=5)
label_key = [Link](frame, text=f"{key}:", font=("Times New Roman", 16, "bold"), bg="light
pink", anchor="w")
label_key.pack(side="left", fill="x")
label_value = [Link](frame, text=value, font=("Times New Roman", 16), bg="light pink",
anchor="w")
label_value.pack(side="left", fill="x")
# Back to User Page Button
back_button = [Link](profile_window, text="GO BACK TO USER'S PAGE", font=("Times New
Roman", 16), bg="white", command=profile_window.destroy)
back_button.pack(pady=20)
def edit_account_details(username):
# Open the CSV file and find the user's data
user_data = None
rows = []
with open("[Link]", mode="r") as file:
reader = [Link](file)
for row in reader:
[Link](row)
if row[4] == username: # Assuming the 5th column stores the username
user_data = row
if not user_data:
[Link]("Error", "User data not found!")
return
# Create the Edit Details window
edit_window = [Link]()
edit_window.title("Edit Account Details")
edit_window.geometry("600x600")
edit_window.configure(bg="light pink")
title_label = [Link](edit_window, text="EDIT YOUR ACCOUNT DETAILS", font=("Times New
Roman", 20, "bold"), bg="light pink")
title_label.pack(pady=20)
# Define the fields to edit
fields_to_edit = {
"Full Name": 0,
"Mobile Number": 2,
"Email Address": 3,
"House Number/Building Name": 6,
"Area/Street": 7,
"Country": 8,
"State": 9,
"Pin Code": 10,
entries = {}
for i, (label_text, index) in enumerate(fields_to_edit.items()):
label = [Link](edit_window, text=label_text, font=("Times New Roman", 16), bg="light pink")
[Link](pady=5)
entry = [Link](edit_window, font=("Times New Roman", 16))
[Link](0, user_data[index]) # Pre-fill with existing data
[Link](pady=5)
entries[label_text] = entry
# Save changes function
def save_changes():
updated_data = user_data.copy()
for field, entry in [Link]():
if [Link](): # Update only if the field is not empty
updated_data[fields_to_edit[field]] = [Link]()
# Update the CSV file with the new details
with open("[Link]", mode="w", newline='') as file:
writer = [Link](file)
for row in rows:
if row[4] == username:
[Link](updated_data) # Write updated data
else:
[Link](row)
[Link]("Success", "Account details updated successfully!")
edit_window.destroy()
# Save Changes button
save_button = [Link](edit_window, text="SAVE CHANGES", font=("Times New Roman", 12),
bg="white", command=save_changes)
save_button.pack(pady=20)
# Close the Edit Details window
def close_edit_window():
edit_window.destroy()
close_button = [Link](edit_window, text="CANCEL", font=("Times New Roman", 12),
bg="white", command=close_edit_window)
close_button.pack(pady=10)
def change_password_page():
# Create the Change Password window
change_password_window = [Link]()
change_password_window.title("Change Password")
change_password_window.geometry("600x400")
change_password_window.configure(bg="light pink")
title_label = [Link](change_password_window, text="CHANGE PASSWORD", font=("Times New
Roman", 24, "bold"), bg="light pink")
title_label.pack(pady=20)
# Username
username_label = [Link](change_password_window, text="Username", font=("Times New
Roman", 16), bg="light pink")
username_label.pack(pady=5)
username_entry = [Link](change_password_window, font=("Times New Roman", 16))
username_entry.pack(pady=5)
# New Password
new_password_label = [Link](change_password_window, text="New Password", font=("Times
New Roman", 16), bg="light pink")
new_password_label.pack(pady=5)
new_password_entry = [Link](change_password_window, show="*", font=("Times New Roman",
16))
new_password_entry.pack(pady=5)
# Confirm New Password
confirm_password_label = [Link](change_password_window, text="Confirm New Password",
font=("Times New Roman", 16), bg="light pink")
confirm_password_label.pack(pady=5)
confirm_password_entry = [Link](change_password_window, show="*", font=("Times New
Roman", 16))
confirm_password_entry.pack(pady=5)
# Function to save the new password
def save_new_password():
username = username_entry.get()
new_password = new_password_entry.get()
confirm_password = confirm_password_entry.get()
if not username or not new_password or not confirm_password:
[Link]("Error", "All fields are required!")
return
if new_password != confirm_password:
[Link]("Error", "Passwords do not match!")
return
user_found = False
rows = []
# Check and update the password in the CSV
with open("[Link]", mode="r") as file:
reader = [Link](file)
for row in reader:
if row[4] == username: # Assuming username is in the 5th column
user_found = True
row[5] = new_password # Assuming password is in the 6th column
[Link](row)
if user_found:
with open("[Link]", mode="w", newline='') as file:
writer = [Link](file)
[Link](rows)
[Link]("Success", "Password changed successfully!")
change_password_window.destroy()
else:
[Link]("Error", "Username not found! Please try again.")
# Save Changes button
save_button = [Link](change_password_window, text="SUBMIT", font=("Times New Roman",
16), bg="white", command=save_new_password)
save_button.pack(pady=20)
# Close the Change Password window
def close_change_password_window():
change_password_window.destroy()
cancel_button = [Link](change_password_window, text="CANCEL", font=("Times New Roman",
16), bg="white", command=close_change_password_window)
cancel_button.pack(pady=10)
def delete_account_page(username):
delete_window = [Link]()
delete_window.title("Delete Account")
delete_window.geometry("600x300")
delete_window.configure(bg="light pink")
# Main frame to center content
center_frame = [Link](delete_window, bg="light pink")
center_frame.pack(expand=True) # Expands the frame to fill the window, centering content
# Confirmation message
confirm_label = [Link](center_frame, text="ARE YOU SURE YOU WANT TO PERMANENTLY DELETE
THIS ACCOUNT?",
font=("Times New Roman", 40, "bold"), bg="light pink", wraplength=500)
confirm_label.pack(pady=50)
# YES button: Deletes the account
def delete_account():
user_data = []
account_deleted = False
# Read and filter out the account to be deleted
with open("[Link]", mode="r") as file:
reader = [Link](file)
for row in reader:
if row[4] != username: # Assuming username is in the 5th column (index 4)
user_data.append(row)
else:
account_deleted = True
# Write back filtered data to the CSV file
with open("[Link]", mode="w", newline='') as file:
writer = [Link](file)
[Link](user_data)
if account_deleted:
[Link]("Account Deleted", "Your account has been successfully deleted.")
delete_window.destroy()
else:
[Link]("Error", "Username not found. Please try again.")
# YES Button
yes_button = [Link](center_frame, text="YES", font=("Times New Roman", 25), bg="white",
command=delete_account)
yes_button.pack(pady=30)
# NO Button
no_button = [Link](center_frame, text="NO", font=("Times New Roman", 25), bg="white",
command=delete_window.destroy)
no_button.pack(pady=30)
delete_window.mainloop()
def admin_login_page():
admin_window = [Link]()
admin_window.title("Admin Login")
admin_window.geometry("600x400")
admin_window.configure(bg="light pink")
# Title for Admin Login
title_label = [Link](admin_window, text="WELCOME, ADMIN", font=("Times New Roman", 24,
"bold"), bg="light pink")
title_label.pack(pady=30)
# Admin ID
admin_id_label = [Link](admin_window, text="Admin ID", font=("Times New Roman", 18),
bg="light pink")
admin_id_label.pack(pady=5)
admin_id_entry = [Link](admin_window, font=("Times New Roman", 18))
admin_id_entry.pack(pady=5)
# Security Code
security_code_label = [Link](admin_window, text="Security Code", font=("Times New Roman",
18), bg="light pink")
security_code_label.pack(pady=5)
security_code_entry = [Link](admin_window, show="*", font=("Times New Roman", 18))
security_code_entry.pack(pady=5)
# Function to validate admin credentials
def validate_admin_login():
admin_id = admin_id_entry.get()
security_code = security_code_entry.get()
# Check if admin credentials match
if admin_id == "natswas" and security_code == "123456":
[Link]("Success", "Admin login successful!")
admin_window.destroy()
admin_dashboard_page()
else:
[Link]("Error", "Invalid Admin ID or Security Code. Please try again.")
# Login button
login_button = [Link](admin_window, text="Login", font=("Times New Roman", 18),
bg="white", command=validate_admin_login)
login_button.pack(pady=30)
admin_window.mainloop()
def admin_dashboard_page():
admin_dashboard_window = [Link]()
admin_dashboard_window.title("Admin Dashboard")
admin_dashboard_window.geometry("600x400")
admin_dashboard_window.configure(bg="light pink")
# Title
title_label = [Link](admin_dashboard_window, text="ADMIN DASHBOARD", font=("Times New
Roman", 48, "bold"), bg="light pink")
title_label.pack(pady=50)
# Button to delete a user's account
delete_user_button = [Link](admin_dashboard_window, text="DELETE A USER'S ACCOUNT",
font=("Times New Roman", 30), bg="white", command=delete_user_account)
delete_user_button.pack(pady=35)
# Button to generate a bill for a user
generate_bill_button = [Link](admin_dashboard_window, text="GENERATE A BILL FOR A
USER", font=("Times New Roman", 30), bg="white", command=generate_bill)
generate_bill_button.pack(pady=35)
admin_logout_button = [Link](admin_dashboard_window, text="LOG OUT(ADMIN)",
font=("Times New Roman", 30), bg="white")
admin_logout_button.pack(pady=35)
admin_dashboard_window.mainloop()
def delete_user_account():
delete_user_window = [Link]()
delete_user_window.title("Delete User Account")
delete_user_window.geometry("600x400")
delete_user_window.configure(bg="light pink")
# Title
title_label = [Link](delete_user_window, text="DELETE USER ACCOUNT", font=("Times New
Roman", 48, "bold"), bg="light pink")
title_label.pack(pady=50)
# Label and Entry for username input
username_label = [Link](delete_user_window, text="Enter the username of the user you want
to delete:", font=("Times New Roman", 24), bg="light pink")
username_label.pack(pady=10)
username_entry = [Link](delete_user_window, font=("Times New Roman", 24))
username_entry.pack(pady=10)
# Function to delete the user when 'Delete User' button is pressed
def delete_user():
username_to_delete = username_entry.get().strip() # Remove any leading/trailing spaces
# Open the CSV file and read the contents
try:
with open('[Link]', 'r') as file:
lines = [Link]()
# Write back all lines except the one with the username to be deleted
with open('[Link]', 'w') as file:
user_deleted = False
for line in lines:
# Strip any extra spaces from the stored username (5th column) and compare
if [Link](',')[4].strip() != username_to_delete:
[Link](line)
else:
user_deleted = True
if user_deleted:
[Link]("Success", "USER ACCOUNT DELETED.")
delete_user_window.destroy() # Close the window after successful deletion
else:
[Link]("Error", "Username not found. Please check and try again.")
except FileNotFoundError:
[Link]("Error", "User data file not found.")
# Button to trigger the deletion
delete_button = [Link](delete_user_window, text="DELETE USER", font=("Times New Roman",
24), bg="white", command=delete_user)
delete_button.pack(pady=25)
delete_user_window.mainloop()
def generate_bill():
# Create a new window for generating bill
generate_bill_window = [Link]()
generate_bill_window.title("Generate Bill for a User")
generate_bill_window.geometry("600x500")
generate_bill_window.configure(bg="light pink")
# Title
title_label = [Link](generate_bill_window, text="Generate Bill for a User", font=("Times New
Roman", 30, "bold"), bg="light pink")
title_label.grid(row=0, column=0, columnspan=2, pady=25)
# Username
username_label = [Link](generate_bill_window, text="Enter Username:", font=("Times New
Roman", 20), bg="light pink")
username_label.grid(row=1, column=0, padx=10, pady=10, sticky="e")
username_entry = [Link](generate_bill_window, font=("Times New Roman", 20), bg="white")
username_entry.grid(row=1, column=1, padx=10, pady=10)
# Month
month_label = [Link](generate_bill_window, text="Enter Month:", font=("Times New Roman",
20), bg="light pink")
month_label.grid(row=2, column=0, padx=10, pady=10, sticky="e")
month_entry = [Link](generate_bill_window, font=("Times New Roman", 20), bg="white")
month_entry.grid(row=2, column=1, padx=10, pady=10)
# Year
year_label = [Link](generate_bill_window, text="Enter Year:", font=("Times New Roman", 20),
bg="light pink")
year_label.grid(row=3, column=0, padx=10, pady=10, sticky="e")
year_entry = [Link](generate_bill_window, font=("Times New Roman", 20), bg="white")
year_entry.grid(row=3, column=1, padx=10, pady=10)
# Previous Date of Reading
previous_date_label = [Link](generate_bill_window, text="Enter Previous Date of Reading:",
font=("Times New Roman", 20), bg="light pink")
previous_date_label.grid(row=4, column=0, padx=10, pady=10, sticky="e")
previous_date_entry = [Link](generate_bill_window, font=("Times New Roman", 20),
bg="white")
previous_date_entry.grid(row=4, column=1, padx=10, pady=10)
# Present Date of Reading
present_date_label = [Link](generate_bill_window, text="Enter Present Date of Reading:",
font=("Times New Roman", 20), bg="light pink")
present_date_label.grid(row=5, column=0, padx=10, pady=10, sticky="e")
present_date_entry = [Link](generate_bill_window, font=("Times New Roman", 20), bg="white")
present_date_entry.grid(row=5, column=1, padx=10, pady=10)
# Previous Meter Reading
previous_meter_label = [Link](generate_bill_window, text="Enter Previous Meter Reading:",
font=("Times New Roman", 20), bg="light pink")
previous_meter_label.grid(row=6, column=0, padx=10, pady=10, sticky="e")
previous_meter_entry = [Link](generate_bill_window, font=("Times New Roman", 20),
bg="white")
previous_meter_entry.grid(row=6, column=1, padx=10, pady=10)
present_meter_label = [Link](generate_bill_window, text="Enter Present Meter Reading:",
font=("Times New Roman", 20), bg="light pink")
present_meter_label.grid(row=7, column=0, padx=10, pady=10, sticky="e")
present_meter_entry = [Link](generate_bill_window, font=("Times New Roman", 20),
bg="white")
present_meter_entry.grid(row=7, column=1, padx=10, pady=10)
# Function to save the bill details
def submit_bill():
username = username_entry.get().strip()
month = month_entry.get().strip()
year = year_entry.get().strip()
previous_date = previous_date_entry.get().strip()
present_date = present_date_entry.get().strip()
previous_meter = previous_meter_entry.get().strip()
present_meter = present_meter_entry.get().strip()
# Validation of the inputs
if not (username and month and year and previous_date and present_date and
previous_meter):
[Link]("Error", "Please fill in all the fields!")
return
# Create or open the user's specific file
file_name = f"{username}.csv"
# If the file doesn't exist, create a new one and write the headers
if not [Link](file_name):
with open(file_name, mode='w', newline='') as file:
writer = [Link](file)
[Link](["Username", "Month", "Year", "Previous Date", "Present Date", "Previous
Meter Reading", "Present Meter Reading"])
# Append the entered data to the user's file
with open(file_name, mode='a', newline='') as file:
writer = [Link](file)
[Link]([username, month, year, previous_date, present_date, previous_meter,
present_meter])
# Show success message
[Link]("Success", "READINGS ENTERED SUCCESSFULLY")
generate_bill_window.destroy()
# Submit button
submit_button = [Link](generate_bill_window, text="Submit these entries", font=("Times New
Roman", 24), bg="white", command=submit_bill)
submit_button.grid(row=8, column=0, columnspan=2, pady=25)
generate_bill_window.mainloop()
import tkinter as tk
from tkinter import messagebox
import csv
import os
def view_bills():
# Create a new window for viewing the bill
view_bill_window = [Link]()
view_bill_window.title("View Your Electricity Bill")
view_bill_window.geometry("600x500")
view_bill_window.configure(bg="light pink")
# Title
title_label = [Link](view_bill_window, text="ENTER YEAR, MONTH AND YOUR USERNAME",
font=("Times New Roman", 25), bg="light pink")
title_label.grid(row=0, column=0, columnspan=2, pady=25)
# Username
username_label = [Link](view_bill_window, text="Enter Username:", font=("Times New Roman",
20), bg="light pink")
username_label.grid(row=1, column=0, padx=10, pady=10, sticky="e")
username_entry = [Link](view_bill_window, font=("Times New Roman", 20), bg="white")
username_entry.grid(row=1, column=1, padx=10, pady=10)
# Month
month_label = [Link](view_bill_window, text="Enter Month:", font=("Times New Roman", 20),
bg="light pink")
month_label.grid(row=2, column=0, padx=10, pady=10, sticky="e")
month_entry = [Link](view_bill_window, font=("Times New Roman", 20), bg="white")
month_entry.grid(row=2, column=1, padx=10, pady=10)
# Year
year_label = [Link](view_bill_window, text="Enter Year:", font=("Times New Roman", 20),
bg="light pink")
year_label.grid(row=3, column=0, padx=10, pady=10, sticky="e")
year_entry = [Link](view_bill_window, font=("Times New Roman", 20), bg="white")
year_entry.grid(row=3, column=1, padx=10, pady=10)
# Function to search for the bill details and show the bill in a separate window
def show_bill():
username = username_entry.get().strip()
month = month_entry.get().strip()
year = year_entry.get().strip()
if not (username and month and year):
[Link]("Error", "Please fill in all the fields!")
return
file_name = f"{username}.csv"
# Check if the file exists
if not [Link](file_name):
[Link]("Error", "User data not found!")
return
# Read the user's file to find the relevant bill
with open(file_name, mode='r', newline='') as file:
reader = [Link](file)
found = False
for row in reader:
if row[1] == month and row[2] == year:
found = True
previous_date = row[3]
present_date = row[4]
previous_meter = float(row[5])
present_meter = float(row[6])
# Calculate the amount consumed
amount_consumed = present_meter - previous_meter
# Calculate the charges
fixed_charges = 90
rate_per_kwh = 4.30
variable_charges = amount_consumed * rate_per_kwh
taxes = variable_charges * 0.03
total_payable = variable_charges + taxes
# Calculate last date to pay (add 14 days to the day part of the present date)
# present_date is assumed to be in the format "DD/MM/YYYY" (e.g., "03/10/2019")
day, month, year = map(int, present_date.split('/')) # Splitting the date into day, month,
and year
day += 14 # Add 14 days to the day part
# If day exceeds the number of days in the month, adjust month and year
# For simplicity, we assume all months have 30 days in this example (you can expand this
logic for exact month lengths)
if day > 30:
day -= 30
month += 1
if month > 12:
month = 1
year += 1
# Format the last date to pay as "DD/MM/YYYY"
last_date_to_pay = f"{day:02d}/{month:02d}/{year}"
# Create a new window to display the bill
bill_popup = [Link](view_bill_window)
bill_popup.title("ELECTRICITY BILL")
bill_popup.geometry("600x500")
bill_popup.configure(bg="light pink")
bill_content = f"""
ELECTRICITY BILL
MONTH: {month}
YEAR: {year}
PREVIOUS DATE OF READING: {previous_date}
PRESENT DATE OF READING: {present_date}
PREVIOUS METER READING: {previous_meter} kWh
PRESENT METER READING: {present_meter} kWh
AMOUNT CONSUMED: {amount_consumed} kWh
Fixed charges: 90 Rs
Rate Per KWH: 4.30 Rs
VARIABLE CHARGES: {variable_charges} Rs
TAXES: {taxes} Rs
TOTAL PAYABLE AMOUNT: {total_payable} Rs
LAST DATE TO PAY: {last_date_to_pay}
"""
bill_label = [Link](bill_popup, text=bill_content, font=("Times New Roman", 18),
bg="light pink", justify="left")
bill_label.pack(pady=25)
# Back button to close the bill popup
back_button = [Link](bill_popup, text="Back", font=("Times New Roman", 18),
bg="white", command=bill_popup.destroy)
back_button.pack(pady=10)
break
if not found:
[Link]("Error", "No bill found for the given month and year.")
# Button to submit the details and generate the bill
submit_button = [Link](view_bill_window, text="Generate Bill", font=("Times New Roman",
24), bg="white", command=show_bill)
submit_button.grid(row=4, column=0, columnspan=2, pady=25)
# Button to go back to the user's page
back_button = [Link](view_bill_window, text="Back to User's Page", font=("Times New
Roman", 24), bg="white", command=view_bill_window.destroy)
back_button.grid(row=5, column=0, columnspan=2, pady=25)
view_bill_window.mainloop()
# Start the application
first_page()