Amazing-Python-Scripts
411 строк · 13.4 Кб
1from tkinter import *2import math3
4values = []5vec_window_count = []6
7
8def func_main():9""" Get the Variables to calculate relations """10
11global Vec_112global Vec_213global values14
15Vals = [V1_x_val.get(), V1_y_val.get(), V1_z_val.get(),16V2_x_val.get(), V2_y_val.get(), V2_z_val.get()]17# Validate values18try:19for i in range(6):20Vals[i] = float(Vals[i])21except:22SetStatusError()23return24
25DotProduct(Vals)26CrossProduct(Vals)27Angle(Vals)28comp_v_on_v(Vals)29proj_v_on_v(Vals)30
31Vec_1.place(x=355, y=10)32Vec_2.place(x=355, y=40)33values = Vals34
35
36def DotProduct(Vals):37""" Dot product of 2 vectors """38res = (Vals[0]*Vals[3]) + (Vals[1]*Vals[4]) + (Vals[2]*Vals[5])39dotproduct_entry_val.set(res)40return res41
42
43def CrossProduct(Vals):44""" Cross product of 2 vectors """45res_x = (Vals[1] * Vals[5]) - (Vals[4] * Vals[2])46res_y = (Vals[0] * Vals[5]) - (Vals[3] * Vals[2])47res_z = (Vals[0] * Vals[4]) - (Vals[1] * Vals[3])48crossproduct_x_val.set(res_x)49crossproduct_y_val.set(res_y)50crossproduct_z_val.set(res_z)51
52
53def abs_val(Vals):54""" Absolute value of a vector |v| """55res = (Vals[0]**2 + Vals[1]**2 + Vals[2]**2)**0.556return res57
58
59def Angle(Vals):60""" Angle between both vectors """61abs_v1 = abs_val(Vals[:3])62abs_v2 = abs_val(Vals[3:])63dot = DotProduct(Vals)64try:65ang = round(math.acos(dot / (abs_v1*abs_v2)) * 180 / math.pi, 5)66except:67ang = "Invalid"68angle_val.set(ang)69
70
71def comp_v_on_v(Vals):72""" Compnent of a vector on the other """73dot_prod = DotProduct(Vals)74abs_v1 = abs_val(Vals[:3])75abs_v2 = abs_val(Vals[3:])76
77try:78res_a_on_b = round(dot_prod / abs_v2, 5)79a_on_b_val.set(res_a_on_b)80except:81a_on_b_val.set("Invalid")82
83try:84res_b_on_a = round(dot_prod / abs_v1, 5)85b_on_a_val.set(res_b_on_a)86except:87b_on_a_val.set("Invalid")88
89
90def proj_v_on_v(Vals):91""" Projection of a vector on the other """92dot_prod = DotProduct(Vals)93abs_v1 = abs_val(Vals[:3])94abs_v2 = abs_val(Vals[3:])95
96try:97res_a_on_b = round(dot_prod / abs_v2**2, 5)98x_1 = res_a_on_b * Vals[3]99y_1 = res_a_on_b * Vals[4]100z_1 = res_a_on_b * Vals[5]101a_on_b_proj_x_val.set(x_1)102a_on_b_proj_y_val.set(y_1)103a_on_b_proj_z_val.set(z_1)104except:105a_on_b_proj_x_val.set("Invalid")106a_on_b_proj_y_val.set("Invalid")107a_on_b_proj_z_val.set("Invalid")108
109try:110res_b_on_a = round(dot_prod / abs_v1**2, 5)111x_2 = res_b_on_a * Vals[0]112y_2 = res_b_on_a * Vals[1]113z_2 = res_b_on_a * Vals[2]114b_on_a_proj_x_val.set(x_2)115b_on_a_proj_y_val.set(y_2)116b_on_a_proj_z_val.set(z_2)117except:118b_on_a_proj_x_val.set("Invalid")119b_on_a_proj_y_val.set("Invalid")120b_on_a_proj_z_val.set("Invalid")121
122Status["text"] = "Calculations Completed! :D "123Status["fg"] = "green"124
125
126def SetStatusError():127""" Sets Status bar label to error message """128Status["text"] = "Wronge Input(s)... :\ "129Status["fg"] = "red"130
131
132def on_closing():133""" Closes all Instances """134global vec_window_count135global main136
137try:138for window in vec_window_count:139window.destroy()140main.destroy()141except:142main.destroy()143
144
145# <----- Single Vector Properties GUI-Backend Code Block Start ----->
146def Show_Vec_Frame(vec_num, values):147""" Shows the properties of a single vector """148global vec_window_count149
150if vec_num == 1:151values = values[:3]152title = "Vector A Properties"153else:154values = values[3:]155title = "Vector B Properties"156
157vec_window = Tk()158vec_window.title(title)159vec_window.geometry("300x250")160vec_window_count.append(vec_window)161
162# Modulus163Modulus = round((values[0]**2 + values[1]**2 + values[2]**2)**0.5, 5)164Modulus_lbl = Label(vec_window, text="Modulus > ",165anchor=E, font=("Calibri", 8))166Modulus_val = Text(vec_window, height=1, borderwidth=0)167Modulus_val.insert(1.0, str(Modulus))168Modulus_lbl.place(x=10, y=10)169Modulus_val.place(x=70, y=11, width=80)170
171# Unit Vectors172try:173uv_x = round(values[0]/Modulus, 5)174uv_y = round(values[1]/Modulus, 5)175uv_z = round(values[2]/Modulus, 5)176except:177uv_x = "Invalid"178uv_y = "Invalid"179uv_z = "Invalid"180
181Unit_Vector_lbl = Label(182vec_window, text="Unit Vector: ", anchor=E, font=("Calibri", 8))183uv_x_lbl = Label(vec_window, text="X > ", anchor=E, font=("Calibri", 8))184uv_x_val = Text(vec_window, height=1, borderwidth=0)185uv_x_val.insert(1.0, str(uv_x))186uv_y_lbl = Label(vec_window, text="Y > ", anchor=E, font=("Calibri", 8))187uv_y_val = Text(vec_window, height=1, borderwidth=0)188uv_y_val.insert(1.0, str(uv_y))189uv_z_lbl = Label(vec_window, text="Z > ", anchor=E, font=("Calibri", 8))190uv_z_val = Text(vec_window, height=1, borderwidth=0)191uv_z_val.insert(1.0, str(uv_z))192Unit_Vector_lbl.place(x=10, y=30)193uv_x_lbl.place(x=25, y=50)194uv_x_val.place(x=50, y=51, width=80)195uv_y_lbl.place(x=25, y=70)196uv_y_val.place(x=50, y=71, width=80)197uv_z_lbl.place(x=25, y=90)198uv_z_val.place(x=50, y=91, width=80)199
200if uv_x != "Invalid":201alpha = round(math.acos(uv_x) * 180 / math.pi, 5)202beta = round(math.acos(uv_y) * 180 / math.pi, 5)203gamma = round(math.acos(uv_z) * 180 / math.pi, 5)204else:205alpha = "Invalid"206beta = "Invalid"207gamma = "Invalid"208Cosine_lbl = Label(vec_window, text="Cosine Angles: ",209anchor=E, font=("Calibri", 8))210alpha_lbl = Label(vec_window, text="X > ", anchor=E, font=("Calibri", 8))211alpha_val = Text(vec_window, height=1, borderwidth=0)212alpha_val.insert(1.0, str(alpha))213beta_lbl = Label(vec_window, text="Y > ", anchor=E, font=("Calibri", 8))214beta_val = Text(vec_window, height=1, borderwidth=0)215beta_val.insert(1.0, str(beta))216gamma_lbl = Label(vec_window, text="Z > ", anchor=E, font=("Calibri", 8))217gamma_val = Text(vec_window, height=1, borderwidth=0)218gamma_val.insert(1.0, str(gamma))219Cosine_lbl.place(x=10, y=120)220alpha_lbl.place(x=25, y=140)221alpha_val.place(x=50, y=141, width=80)222beta_lbl.place(x=25, y=160)223beta_val.place(x=50, y=161, width=80)224gamma_lbl.place(x=25, y=180)225gamma_val.place(x=50, y=181, width=80)226
227vec_window.mainloop()228# <----- Single Vector Properties GUI-Backend Code Block End ----->
229
230
231# <----- GUI Code Block Start ----->
232# Main Window
233main = Tk()234main.title("Vector Calculator")235main.geometry("460x310")236main.protocol("WM_DELETE_WINDOW", on_closing)237
238# Entry Titles
239x_lbl = Label(main, text="X", font=("Calibri", 8))240y_lbl = Label(main, text="Y", font=("Calibri", 8))241z_lbl = Label(main, text="Z", font=("Calibri", 8))242x_lbl.place(x=110, y=15)243y_lbl.place(x=170, y=15)244z_lbl.place(x=230, y=15)245
246# Vector 1
247V1_lbl = Label(main, text="Vector A > ", anchor=E, font=("Calibri", 8))248V1_x_val = StringVar()249V1_x = Entry(main, textvariable=V1_x_val, font=("Calibri", 8))250V1_y_val = StringVar()251V1_y = Entry(main, textvariable=V1_y_val, font=("Calibri", 8))252V1_z_val = StringVar()253V1_z = Entry(main, textvariable=V1_z_val, font=("Calibri", 8))254V1_lbl.place(x=20, y=35)255V1_x.place(x=90, y=36, width=50)256V1_y.place(x=150, y=36, width=50)257V1_z.place(x=210, y=36, width=50)258
259# Vector 2
260V2_lbl = Label(main, text="Vector B > ", anchor=E, font=("Calibri", 8))261V2_x_val = StringVar()262V2_x = Entry(main, textvariable=V2_x_val, font=("Calibri", 8))263V2_y_val = StringVar()264V2_y = Entry(main, textvariable=V2_y_val, font=("Calibri", 8))265V2_z_val = StringVar()266V2_z = Entry(main, textvariable=V2_z_val, font=("Calibri", 8))267V2_lbl.place(x=20, y=65)268V2_x.place(x=90, y=66, width=50)269V2_y.place(x=150, y=66, width=50)270V2_z.place(x=210, y=66, width=50)271
272# Calculate Button
273Calculate = Button(main, text="Calculate", font=(274"Calibri", 8), command=func_main)275Calculate.place(x=270, y=48, width=70)276
277# Results Frame ----->
278frame = Frame(main, bg="#708090")279frame.place(x=20, y=90, width=420, height=197)280
281# Dot Product
282dotproduct_lbl = Label(frame, text="Dot Product:", anchor=W,283bg="#708090", fg="black", font=("Calibri", 8))284dotproduct_lbl.place(x=10, y=10)285dotproduct_entry_val = StringVar()286dotproduct_entry = Entry(287frame, textvariable=dotproduct_entry_val, font=("Calibri", 8))288dotproduct_entry.configure(state='readonly')289dotproduct_entry.place(x=80, y=11, width=50)290
291# Cross Product
292crossproduct_lbl = Label(frame, text="Cross Product:",293anchor=W, bg="#708090", fg="black", font=("Calibri", 8))294crossproduct_lbl.place(x=10, y=40)295crossproduct_x_lbl = Label(296frame, text="X (i) > ", anchor=E, bg="#708090", fg="black", font=("Calibri", 8))297crossproduct_y_lbl = Label(298frame, text="Y (j) > ", anchor=E, bg="#708090", fg="black", font=("Calibri", 8))299crossproduct_z_lbl = Label(300frame, text="Z (k) > ", anchor=E, bg="#708090", fg="black", font=("Calibri", 8))301crossproduct_x_lbl.place(x=30, y=60)302crossproduct_y_lbl.place(x=30, y=90)303crossproduct_z_lbl.place(x=30, y=120)304crossproduct_x_val = StringVar()305crossproduct_y_val = StringVar()306crossproduct_z_val = StringVar()307crossproduct_x = Entry(308frame, textvariable=crossproduct_x_val, font=("Calibri", 8))309crossproduct_x.configure(state='readonly')310crossproduct_x.place(x=65, y=61, width=50)311crossproduct_y = Entry(312frame, textvariable=crossproduct_y_val, font=("Calibri", 8))313crossproduct_y.configure(state='readonly')314crossproduct_y.place(x=65, y=91, width=50)315crossproduct_z = Entry(316frame, textvariable=crossproduct_z_val, font=("Calibri", 8))317crossproduct_z.configure(state='readonly')318crossproduct_z.place(x=65, y=121, width=50)319
320# Angle between both vectors
321angle_lbl = Label(frame, text="Angle:", anchor=W,322bg="#708090", fg="black", font=("Calibri", 8))323angle_lbl.place(x=10, y=160)324angle_val = StringVar()325angle = Entry(frame, textvariable=angle_val, font=("Calibri", 8))326angle.configure(state='readonly')327angle.place(x=50, y=161, width=80)328
329# Components
330component_lbl = Label(frame, text="Components:", anchor=W,331bg="#708090", fg="black", font=("Calibri", 8))332component_lbl.place(x=170, y=10)333a_on_b_lbl = Label(frame, text="A on B:", anchor=W,334bg="#708090", fg="black", font=("Calibri", 8))335a_on_b_val = StringVar()336a_on_b_ent = Entry(frame, textvariable=a_on_b_val, font=("Calibri", 8))337a_on_b_ent.configure(state='readonly')338b_on_a_lbl = Label(frame, text="B on A:", anchor=W,339bg="#708090", fg="black", font=("Calibri", 8))340b_on_a_val = StringVar()341b_on_a_ent = Entry(frame, textvariable=b_on_a_val, font=("Calibri", 8))342b_on_a_ent.configure(state='readonly')343a_on_b_lbl.place(x=190, y=30)344a_on_b_ent.place(x=230, y=31, width=50)345b_on_a_lbl.place(x=190, y=60)346b_on_a_ent.place(x=230, y=61, width=50)347
348# Projection
349comp_per_lbl = Label(frame, text="Projection:", anchor=W,350bg="#708090", fg="black", font=("Calibri", 8))351a_on_b_proj_lbl = Label(frame, text="A on B:", anchor=W,352bg="#708090", fg="black", font=("Calibri", 8))353b_on_a_proj_lbl = Label(frame, text="B on A:", anchor=W,354bg="#708090", fg="black", font=("Calibri", 8))355res_x_lbl = Label(frame, text="X", anchor=W, bg="#708090",356fg="black", font=("Calibri", 8))357res_y_lbl = Label(frame, text="Y", anchor=W, bg="#708090",358fg="black", font=("Calibri", 8))359res_z_lbl = Label(frame, text="Z", anchor=W, bg="#708090",360fg="black", font=("Calibri", 8))361comp_per_lbl.place(x=170, y=90)362a_on_b_proj_lbl.place(x=190, y=130)363b_on_a_proj_lbl.place(x=190, y=160)364res_x_lbl.place(x=250, y=110)365res_y_lbl.place(x=310, y=110)366res_z_lbl.place(x=370, y=110)367a_on_b_proj_x_val = StringVar()368a_on_b_proj_x = Entry(369frame, textvariable=a_on_b_proj_x_val, font=("Calibri", 8))370a_on_b_proj_x.configure(state='readonly')371a_on_b_proj_y_val = StringVar()372a_on_b_proj_y = Entry(373frame, textvariable=a_on_b_proj_y_val, font=("Calibri", 8))374a_on_b_proj_y.configure(state='readonly')375a_on_b_proj_z_val = StringVar()376a_on_b_proj_z = Entry(377frame, textvariable=a_on_b_proj_z_val, font=("Calibri", 8))378a_on_b_proj_z.configure(state='readonly')379a_on_b_proj_x.place(x=230, y=131, width=50)380a_on_b_proj_y.place(x=290, y=131, width=50)381a_on_b_proj_z.place(x=350, y=131, width=50)382
383b_on_a_proj_x_val = StringVar()384b_on_a_proj_x = Entry(385frame, textvariable=b_on_a_proj_x_val, font=("Calibri", 8))386b_on_a_proj_x.configure(state='readonly')387b_on_a_proj_y_val = StringVar()388b_on_a_proj_y = Entry(389frame, textvariable=b_on_a_proj_y_val, font=("Calibri", 8))390b_on_a_proj_y.configure(state='readonly')391b_on_a_proj_z_val = StringVar()392b_on_a_proj_z = Entry(393frame, textvariable=b_on_a_proj_z_val, font=("Calibri", 8))394b_on_a_proj_z.configure(state='readonly')395b_on_a_proj_x.place(x=230, y=161, width=50)396b_on_a_proj_y.place(x=290, y=161, width=50)397b_on_a_proj_z.place(x=350, y=161, width=50)398
399# Single vector button entry point
400Vec_1 = Button(main, text="Vec A Properties", font=(401"Calibri", 9), command=lambda: Show_Vec_Frame(1, values))402Vec_2 = Button(main, text="Vec B Properties", font=(403"Calibri", 9), command=lambda: Show_Vec_Frame(2, values))404
405# Status Bar
406Status = Label(main, text="Hello!! :D", fg="green", font=(407"Calibri", 8), bd=1, relief=SUNKEN, anchor=W, padx=3)408Status.pack(side=BOTTOM, fill=X)409
410main.mainloop()411# <----- GUI Code Block End ----->
412