-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal_app(2).py
169 lines (138 loc) · 6.75 KB
/
final_app(2).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import tkinter as tk
from tkinter import simpledialog, messagebox
import math
import os
import datetime
USER_DATA_FILE = 'user_data.txt'
LOGIN_DATA_FILE = 'login_data.txt'
def draw_triangle(canvas, origin, length, angle1,angle2,angle3):
# Draw the horizontal line (0 degrees)
canvas.delete("all")
canvas.create_line(origin[0], origin[1], origin[0] + length, origin[1], fill="blue")
# Draw the angled line (60 degrees)
angle_rad = math.radians(angle1) # Convert degrees to radians
x3 = origin[0] + length * math.cos(angle_rad)
y3 = origin[1] - length * math.sin(angle_rad)
canvas.create_line(origin[0], origin[1], x3, y3, fill="blue")
angle_rad1 = math.radians(angle2)
hypo1 = math.hypot(length,length)
canvas.create_line(origin[0] + length, origin[1],x3,y3, fill="blue")
# Label the angle
if angle1 == 90 or angle2 == 90 or angle3 == 90:
canvas.create_text(origin[0],origin[1], text=f'{angle3}°', fill='black')
else:
canvas.create_text(x3,y3, text=f'{angle3}°', fill='black')
def calculate_triangle_angle(canvas, result_label):
try:
angle1 = int(simpledialog.askstring("Input", "Enter the first angle:"))
angle2 = int(simpledialog.askstring("Input", "Enter the second angle:"))
if angle1 <= 0 or angle2 <= 0:
raise ValueError
if angle1 + angle2 >= 180:
messagebox.showerror("Error", "The sum of two angles must be less than 180 degrees.")
else:
angle3 = 180 - (angle1 + angle2)
result_label.config(text=f"The third angle is {angle3} degrees.")
#draw_triangle(canvas, [angle1, angle2, angle3])
if angle1==90 or angle2==90 or angle3 ==90 :
origin = (150, 150)
length = 100
draw_triangle(canvas, origin, length, 90, angle2,90)
else:
origin = (150, 150)
length = 100
draw_triangle(canvas, origin, length, angle1, angle2,angle3)
except ValueError:
messagebox.showerror("Error", "Please enter valid positive integers for angles.")
def draw_quadrilateral(canvas,origin,length,angle1,angle2):
# Draw the horizontal line (0 degrees)
canvas.delete("all")
canvas.create_line(origin[0], origin[1], origin[0] + length, origin[1], fill="blue")
# Draw the angled line (60 degrees)
angle_rad = math.radians(angle1) # Convert degrees to radians
x3 = origin[0] + length * math.cos(angle_rad)
y3 = origin[1] - length * math.sin(angle_rad)
canvas.create_line(origin[0], origin[1], x3, y3, fill="blue")
angle_rad1 =math.radians(angle2)
x4 = origin[0] + length + length*math.cos(angle_rad1)
y4 = origin[1] - length * math.sin(angle_rad1)
canvas.create_line(origin[0]+length,origin[1],x4,y4,fill="blue")
canvas.create_line(x3,y3,x4,y4,fill="blue")
# Label the angle
canvas.create_text(origin[0] + 15, origin[1] - 10, text=f'{angle1}°', fill='black')
def calculate_quadrilateral_angle(canvas, result_label):
try:
angle1 = int(simpledialog.askstring("Input", "Enter the first angle:"))
angle2 = int(simpledialog.askstring("Input", "Enter the second angle:"))
angle3 = int(simpledialog.askstring("Input", "Enter the third angle:"))
if angle1 <= 0 or angle2 <= 0 or angle3 <= 0 or angle1 >= 180 or angle2>=180 or angle3>=180:
raise ValueError
if angle1 + angle2 + angle3 >= 360:
messagebox.showerror("Error", "The sum of three angles must be less than 360 degrees.")
else:
angle4 = 360 - (angle1 + angle2 + angle3)
result_label.config(text=f"The fourth angle is {angle4} degrees.")
origin = 150,150
length = 100
draw_quadrilateral(canvas,origin,length,angle1,angle2)
except ValueError:
messagebox.showerror("Error", "Please enter valid positive integers for angles.")
def log_login_attempt(username):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(LOGIN_DATA_FILE, 'a') as file:
file.write(f"{username},{timestamp}\n")
def main_window():
for widget in root.winfo_children():
widget.destroy() # Remove all widgets from the root window
canvas = tk.Canvas(root, width=400, height=400, bg='white')
canvas.pack()
root.title("Polygon Angle Calculator")
result_label = tk.Label(root, text="", font=("Arial", 14), bg='lightblue')
result_label.pack(pady=10)
tk.Label(root, text="Polygon Angle Calculator", font=("Arial", 16, "bold"), bg='lightblue').pack(pady=10)
tk.Button(root, text="Triangle", command=lambda: calculate_triangle_angle(canvas, result_label),
font=("Arial", 12), bg='lightgreen').pack(pady=5)
tk.Button(root, text="Quadrilateral", command=lambda: calculate_quadrilateral_angle(canvas, result_label),
font=("Arial", 12),
bg='lightcoral').pack(pady=5)
tk.Button(root, text="Exit", command=root.quit, font=("Arial", 12), bg='salmon').pack(pady=10)
def save_user_data(username, email, password):
with open(USER_DATA_FILE, 'a') as file:
file.write(f"{username},{email},{password}\n")
def load_user_data():
if not os.path.exists(USER_DATA_FILE):
return {}
users = {}
with open(USER_DATA_FILE, 'r') as file:
for line in file:
username, email, password = line.strip().split(',')
users[username] = (email, password)
return users
def sign_up():
username = simpledialog.askstring("Sign Up", "Enter username:")
email = simpledialog.askstring("Sign Up", "Enter email:")
password = simpledialog.askstring("Sign Up", "Enter password:", show='*')
if username and email and password:
save_user_data(username, email, password)
messagebox.showinfo("Success", "Sign up successful! You can now log in.")
login_window()
else:
messagebox.showerror("Error", "Please fill all fields.")
def login_window():
username = simpledialog.askstring("Login", "Enter Username or Email:")
password = simpledialog.askstring("Login", "Enter Password:", show='*')
users = load_user_data()
if username in users and users[username][1] == password:
log_login_attempt(username)
messagebox.showinfo("Success", "Login successful!")
main_window()
else:
messagebox.showerror("Error", "Invalid username/email or password.")
# Start Screen
root = tk.Tk()
root.configure(bg='lightblue')
root.title("Welcome")
tk.Label(root, text="Polygon Angle Calculator", font=("Arial", 20), bg='lightblue').pack(pady=20)
tk.Button(root, text="Log In", command=login_window, font=("Arial", 12), bg='lightgreen').pack(pady=10)
tk.Button(root, text="Sign Up", command=sign_up, font=("Arial", 12), bg='lightcoral').pack(pady=10)
root.mainloop()