Yosup 21
Yosup 21
py
import mysql.connector
from mysql.connector import Error
from tkinter import *
from tkinter import messagebox, ttk
import requests
import json
# Connect to MySQL
def connect_to_database():
try:
connection = mysql.connector.connect(
host='localhost',
user='vivek',
password='12345',
database='weather_app'
)
if connection.is_connected():
return connection
except Error as e:
messagebox.showerror("Database Error", f"Error
connecting to MySQL: {e}")
return None
# Function to handle sign-up
def sign_up():
username = username_entry.get()
password = password_entry.get()
confirm_password = confirm_password_entry.get()
connection = connect_to_database()
if not connection:
return
try:
cursor = connection.cursor()
# Check if username already exists
cursor.execute("SELECT username FROM users
WHERE username = %s", (username,))
if cursor.fetchone():
messagebox.showerror("Error", "Username
already exists!")
return
# Insert new user into database
cursor.execute("INSERT INTO users (username,
password) VALUES (%s, %s)", (username, password))
connection.commit()
messagebox.showinfo("Success", "Sign-Up
Successful!")
sign_up_window.destroy()
show_login_page()
except Error as e:
messagebox.showerror("Database Error",
f"Error during sign-up: {e}")
finally:
connection.close()
# Function to handle login
def login():
username = username_entry.get()
password = password_entry.get()
connection = connect_to_database()
if not connection:
return
try:
cursor = connection.cursor()
def show_login_page():
global login_window, username_entry,
password_entry
login_window = Tk()
login_window.title("Login")
login_window.geometry("400x400")
Label(login_window, text="Login",
font=("Arial", 24, "bold")).pack(pady=20)
Label(login_window, text="Username",
font=("Arial", 14)).pack(pady=5)
username_entry = Entry(login_window,
font=("Arial", 14))
username_entry.pack(pady=5)
Label(login_window, text="Password",
font=("Arial", 14)).pack(pady=5)
password_entry = Entry(login_window,
font=("Arial", 14), show="*")
password_entry.pack(pady=5)
Button(login_window, text="Login",
font=("Arial", 14), command=login).pack(pady=20)
Button(login_window, text="Sign Up",
font=("Arial", 14),
command=show_sign_up_page).pack(pady=5)
login_window.mainloop()
# Function to open the Sign-Up page
def show_sign_up_page():
global sign_up_window, username_entry,
password_entry, confirm_password_entry
login_window.destroy()
sign_up_window = Tk()
sign_up_window.title("Sign-Up")
sign_up_window.geometry("400x400")
Label(sign_up_window, text="Sign-Up",
font=("Arial", 24, "bold")).pack(pady=20)
Label(sign_up_window, text="Username",
font=("Arial", 14)).pack(pady=5)
username_entry = Entry(sign_up_window,
font=("Arial", 14))
username_entry.pack(pady=5)
Label(sign_up_window, text="Password",
font=("Arial", 14)).pack(pady=5)
password_entry = Entry(sign_up_window,
font=("Arial", 14), show="*")
password_entry.pack(pady=5)
Label(sign_up_window, text="Confirm Password",
font=("Arial", 14)).pack(pady=5)
confirm_password_entry = Entry(sign_up_window,
font=("Arial", 14), show="*")
confirm_password_entry.pack(pady=5)
Button(sign_up_window, text="Sign Up",
font=("Arial", 14), command=sign_up).pack(pady=20)
sign_up_window.mainloop()
# Weather App Functions
def data_get():
city = city_name.get()
data = requests.get(
f"https://api.openweathermap.org/data/2.5/weathe
r?q={city}&appid=a4cf8acf9c4e4a5be39d9575b2097297").json
()
if data.get("cod") != 200:
w_label1.config(text="Invalid City")
wb_label1.config(text="")
temp_label1.config(text="")
per_label1.config(text="")
humidity_label1.config(text="")
wind_label1.config(text="")
visibility_label1.config(text="")
return
w_label1.config(text=data["weather"][0]["main"])
wb_label1.config(text=data["weather"][0]["descriptio
n"])
temp_label1.config(text=str(int(data["main"]["temp"]
- 273.15)) + "°C")
per_label1.config(text=str(data["main"]["pressure"])
+ " hPa")
humidity_label1.config(text=str(data["main"]["humidi
ty"]) + " %")
wind_label1.config(text=str(data["wind"]["speed"]) +
" m/s")
visibility_label1.config(text=str(data["visibility"]
// 1000) + " km")
def save_data():
city = city_name.get()
weather = w_label1.cget("text")
description = wb_label1.cget("text")
temperature = temp_label1.cget("text")
pressure = per_label1.cget("text")
humidity = humidity_label1.cget("text")
wind_speed = wind_label1.cget("text")
visibility = visibility_label1.cget("text")
connection = connect_to_database()
if not connection:
return
try:
cursor = connection.cursor()
# Insert weather data into the database
cursor.execute(
"INSERT INTO weather_data (city_name,
weather, description, temperature, pressure, humidity,
wind_speed, visibility) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(city, weather, description, temperature,
pressure, humidity, wind_speed, visibility)
)
connection.commit()
messagebox.showinfo("Success", "Weather data
saved to database!")
except Error as e:
messagebox.showerror("Database Error", f"Error
saving weather data: {e}")
finally:
connection.close()
# Start the login page
show_login_page()
def open_weather_app():
global city_name, w_label1, wb_label1, temp_label1,
per_label1, humidity_label1, wind_label1,
visibility_label1
win = Tk()
win.title("AeroMeter")
win.geometry("1408x704")
# Function to open the weather
application
def data_get():
city = city_name.get()
data = requests.get(
f"https://api.openweathermap.org/data/2.5/weathe
r?q={city}&appid=a4cf8acf9c4e4a5be39d9575b2097297").json
()
if data.get("cod") != 200:
w_label1.config(text="Invalid City")
wb_label1.config(text="")
temp_label1.config(text="")
per_label1.config(text="")
humidity_label1.config(text="")
wind_label1.config(text="")
visibility_label1.config(text="")
return
w_label1.config(text=data["weather"][0]["main"])
wb_label1.config(text=data["weather"][0]["descriptio
n"])
temp_label1.config(text=str(int(data["main"]["temp"]
- 273.15)) + "°C")
per_label1.config(text=str(data["main"]["pressure"])
+ " hPa")
humidity_label1.config(text=str(data["main"]["humidi
ty"]) + " %")
wind_label1.config(text=str(data["wind"]["speed"]) +
" m/s")
visibility_label1.config(text=str(data["visibility"]
// 1000) + " km")
def clear_fields():
city_name.set("")
w_label1.config(text="")
wb_label1.config(text="")
temp_label1.config(text="")
per_label1.config(text="")
humidity_label1.config(text="")
wind_label1.config(text="")
visibility_label1.config(text="")
def save_data():
data = {
"city": city_name.get(),
"weather": w_label1.cget("text"),
"description": wb_label1.cget("text"),
"temperature": temp_label1.cget("text"),
"pressure": per_label1.cget("text"),
"humidity": humidity_label1.cget("text"),
"wind_speed": wind_label1.cget("text"),
"visibility": visibility_label1.cget("text")
}
with open("weather_data.txt", "w") as file:
file.write(json.dumps(data))
def load_data():
try:
with open("weather_data.txt", "r") as file:
data = json.load(file)
city_name.set(data["city"])
w_label1.config(text=data["weather"])
wb_label1.config(text=data["description"])
temp_label1.config(text=data["temperature"])
per_label1.config(text=data["pressure"])
humidity_label1.config(text=data["humidity"])
wind_label1.config(text=data["wind_speed"])
visibility_label1.config(text=data["visibility
"])
except FileNotFoundError:
print("No saved data found.")
def refresh_data():
data_get()
win = Tk()
win.title("AeroMeter")
win.geometry("1408x704")
# Adding Background Image:
bg_image =
PhotoImage(file="D:\\pythonproject101\\projectpython23\\
img.png")
bg_label = Label(win, image=bg_image)
bg_label.place(relwidth=1, relheight=1)
name_label = Label(win, text="AeroMeter", font=("Times
New Roman", 28, "bold"), bg="lightblue")
name_label.place(x=25, y=50, height=50, width=450)
city_name = StringVar()
List_name = [
"Andhra Pradesh", "Arunachal Pradesh", "Assam",
"Bihar", "Chhattisgarh", "Goa", "Gujarat",
"Haryana", "Himachal Pradesh", "Jammu and Kashmir",
"Jharkhand", "Karnataka", "Kerala",
"Madhya Pradesh", "Maharashtra", "Manipur",
"Meghalaya", "Mizoram", "Nagaland", "Odisha",
"Punjab", "Rajasthan", "Sikkim", "Tamil Nadu",
"Telangana", "Tripura", "Uttar Pradesh",
"Uttarakhand", "West Bengal", "Andaman and Nicobar
Islands", "Chandigarh", "Dadra and Nagar Haveli",
"Daman and Diu", "Lakshadweep", "National Capital
Territory of Delhi", "Puducherry"
]
com = ttk.Combobox(win, values=List_name, font=("Times
New Roman", 20, "bold"), textvariable=city_name)
com.place(x=25, y=120, height=50, width=450)
w_label = Label(win, text="Weather Climate",
font=("Times New Roman", 20), bg="lightblue")
w_label.place(x=25, y=260, height=50, width=210)
w_label1 = Label(win, text="", font=("Times New
Roman", 20), bg="lightblue")
w_label1.place(x=250, y=260, height=50, width=210)
wb_label = Label(win, text="Weather Description",
font=("Times New Roman", 17), bg="lightblue")
wb_label.place(x=25, y=320, height=50, width=210)
wb_label1 = Label(win, text="", font=("Times New Roman",
17), bg="lightblue")
wb_label1.place(x=250, y=320, height=50, width=210)
temp_label = Label(win, text="Temperature", font=("Times
New Roman", 20), bg="lightblue")
temp_label.place(x=25, y=380, height=50, width=210)
temp_label1 = Label(win, text="", font=("Times New
Roman", 20), bg="lightblue")
temp_label1.place(x=250, y=380, height=50, width=210)
•Action Buttons:
•Done: Likely confirms the city selection or fetches data based on the input.
•Clear: Resets or clears the input field and any displayed data.
•Refresh: Refreshes the data, perhaps fetching updated weather information
Weather Display
Load Button:
•Purpose: Retrieves and displays saved weather data from the MySQL database.
Comparing accuracy of
data :
• Update Timing: Differences
arise due to varying update
intervals between the app
and the website.
• Data Estimation:
Interpolation methods by
different services can cause
discrepancies.
• Calculations: Differences
in rounding, unit
conversions, or formulas
can impact values.
• Real-Time vs Forecast:
One source might show
real-time data, while the
other shows forecasts.