Amazing-Python-Scripts

Форк
0
411 строк · 13.4 Кб
1
from tkinter import *
2
import math
3

4
values = []
5
vec_window_count = []
6

7

8
def func_main():
9
    """ Get the Variables to calculate relations """
10

11
    global Vec_1
12
    global Vec_2
13
    global values
14

15
    Vals = [V1_x_val.get(), V1_y_val.get(), V1_z_val.get(),
16
            V2_x_val.get(), V2_y_val.get(), V2_z_val.get()]
17
    # Validate values
18
    try:
19
        for i in range(6):
20
            Vals[i] = float(Vals[i])
21
    except:
22
        SetStatusError()
23
        return
24

25
    DotProduct(Vals)
26
    CrossProduct(Vals)
27
    Angle(Vals)
28
    comp_v_on_v(Vals)
29
    proj_v_on_v(Vals)
30

31
    Vec_1.place(x=355, y=10)
32
    Vec_2.place(x=355, y=40)
33
    values = Vals
34

35

36
def DotProduct(Vals):
37
    """ Dot product of 2 vectors """
38
    res = (Vals[0]*Vals[3]) + (Vals[1]*Vals[4]) + (Vals[2]*Vals[5])
39
    dotproduct_entry_val.set(res)
40
    return res
41

42

43
def CrossProduct(Vals):
44
    """ Cross product of 2 vectors """
45
    res_x = (Vals[1] * Vals[5]) - (Vals[4] * Vals[2])
46
    res_y = (Vals[0] * Vals[5]) - (Vals[3] * Vals[2])
47
    res_z = (Vals[0] * Vals[4]) - (Vals[1] * Vals[3])
48
    crossproduct_x_val.set(res_x)
49
    crossproduct_y_val.set(res_y)
50
    crossproduct_z_val.set(res_z)
51

52

53
def abs_val(Vals):
54
    """ Absolute value of a vector |v| """
55
    res = (Vals[0]**2 + Vals[1]**2 + Vals[2]**2)**0.5
56
    return res
57

58

59
def Angle(Vals):
60
    """ Angle between both vectors """
61
    abs_v1 = abs_val(Vals[:3])
62
    abs_v2 = abs_val(Vals[3:])
63
    dot = DotProduct(Vals)
64
    try:
65
        ang = round(math.acos(dot / (abs_v1*abs_v2)) * 180 / math.pi, 5)
66
    except:
67
        ang = "Invalid"
68
    angle_val.set(ang)
69

70

71
def comp_v_on_v(Vals):
72
    """ Compnent of a vector on the other """
73
    dot_prod = DotProduct(Vals)
74
    abs_v1 = abs_val(Vals[:3])
75
    abs_v2 = abs_val(Vals[3:])
76

77
    try:
78
        res_a_on_b = round(dot_prod / abs_v2, 5)
79
        a_on_b_val.set(res_a_on_b)
80
    except:
81
        a_on_b_val.set("Invalid")
82

83
    try:
84
        res_b_on_a = round(dot_prod / abs_v1, 5)
85
        b_on_a_val.set(res_b_on_a)
86
    except:
87
        b_on_a_val.set("Invalid")
88

89

90
def proj_v_on_v(Vals):
91
    """ Projection of a vector on the other """
92
    dot_prod = DotProduct(Vals)
93
    abs_v1 = abs_val(Vals[:3])
94
    abs_v2 = abs_val(Vals[3:])
95

96
    try:
97
        res_a_on_b = round(dot_prod / abs_v2**2, 5)
98
        x_1 = res_a_on_b * Vals[3]
99
        y_1 = res_a_on_b * Vals[4]
100
        z_1 = res_a_on_b * Vals[5]
101
        a_on_b_proj_x_val.set(x_1)
102
        a_on_b_proj_y_val.set(y_1)
103
        a_on_b_proj_z_val.set(z_1)
104
    except:
105
        a_on_b_proj_x_val.set("Invalid")
106
        a_on_b_proj_y_val.set("Invalid")
107
        a_on_b_proj_z_val.set("Invalid")
108

109
    try:
110
        res_b_on_a = round(dot_prod / abs_v1**2, 5)
111
        x_2 = res_b_on_a * Vals[0]
112
        y_2 = res_b_on_a * Vals[1]
113
        z_2 = res_b_on_a * Vals[2]
114
        b_on_a_proj_x_val.set(x_2)
115
        b_on_a_proj_y_val.set(y_2)
116
        b_on_a_proj_z_val.set(z_2)
117
    except:
118
        b_on_a_proj_x_val.set("Invalid")
119
        b_on_a_proj_y_val.set("Invalid")
120
        b_on_a_proj_z_val.set("Invalid")
121

122
    Status["text"] = "Calculations Completed! :D "
123
    Status["fg"] = "green"
124

125

126
def SetStatusError():
127
    """ Sets Status bar label to error message """
128
    Status["text"] = "Wronge Input(s)... :\ "
129
    Status["fg"] = "red"
130

131

132
def on_closing():
133
    """ Closes all Instances """
134
    global vec_window_count
135
    global main
136

137
    try:
138
        for window in vec_window_count:
139
            window.destroy()
140
        main.destroy()
141
    except:
142
        main.destroy()
143

144

145
# <----- Single Vector Properties GUI-Backend Code Block Start ----->
146
def Show_Vec_Frame(vec_num, values):
147
    """ Shows the properties of a single vector """
148
    global vec_window_count
149

150
    if vec_num == 1:
151
        values = values[:3]
152
        title = "Vector A Properties"
153
    else:
154
        values = values[3:]
155
        title = "Vector B Properties"
156

157
    vec_window = Tk()
158
    vec_window.title(title)
159
    vec_window.geometry("300x250")
160
    vec_window_count.append(vec_window)
161

162
    # Modulus
163
    Modulus = round((values[0]**2 + values[1]**2 + values[2]**2)**0.5, 5)
164
    Modulus_lbl = Label(vec_window, text="Modulus > ",
165
                        anchor=E, font=("Calibri", 8))
166
    Modulus_val = Text(vec_window, height=1, borderwidth=0)
167
    Modulus_val.insert(1.0, str(Modulus))
168
    Modulus_lbl.place(x=10, y=10)
169
    Modulus_val.place(x=70, y=11, width=80)
170

171
    # Unit Vectors
172
    try:
173
        uv_x = round(values[0]/Modulus, 5)
174
        uv_y = round(values[1]/Modulus, 5)
175
        uv_z = round(values[2]/Modulus, 5)
176
    except:
177
        uv_x = "Invalid"
178
        uv_y = "Invalid"
179
        uv_z = "Invalid"
180

181
    Unit_Vector_lbl = Label(
182
        vec_window, text="Unit Vector: ", anchor=E, font=("Calibri", 8))
183
    uv_x_lbl = Label(vec_window, text="X > ", anchor=E, font=("Calibri", 8))
184
    uv_x_val = Text(vec_window, height=1, borderwidth=0)
185
    uv_x_val.insert(1.0, str(uv_x))
186
    uv_y_lbl = Label(vec_window, text="Y > ", anchor=E, font=("Calibri", 8))
187
    uv_y_val = Text(vec_window, height=1, borderwidth=0)
188
    uv_y_val.insert(1.0, str(uv_y))
189
    uv_z_lbl = Label(vec_window, text="Z > ", anchor=E, font=("Calibri", 8))
190
    uv_z_val = Text(vec_window, height=1, borderwidth=0)
191
    uv_z_val.insert(1.0, str(uv_z))
192
    Unit_Vector_lbl.place(x=10, y=30)
193
    uv_x_lbl.place(x=25, y=50)
194
    uv_x_val.place(x=50, y=51, width=80)
195
    uv_y_lbl.place(x=25, y=70)
196
    uv_y_val.place(x=50, y=71, width=80)
197
    uv_z_lbl.place(x=25, y=90)
198
    uv_z_val.place(x=50, y=91, width=80)
199

200
    if uv_x != "Invalid":
201
        alpha = round(math.acos(uv_x) * 180 / math.pi, 5)
202
        beta = round(math.acos(uv_y) * 180 / math.pi, 5)
203
        gamma = round(math.acos(uv_z) * 180 / math.pi, 5)
204
    else:
205
        alpha = "Invalid"
206
        beta = "Invalid"
207
        gamma = "Invalid"
208
    Cosine_lbl = Label(vec_window, text="Cosine Angles: ",
209
                       anchor=E, font=("Calibri", 8))
210
    alpha_lbl = Label(vec_window, text="X > ", anchor=E, font=("Calibri", 8))
211
    alpha_val = Text(vec_window, height=1, borderwidth=0)
212
    alpha_val.insert(1.0, str(alpha))
213
    beta_lbl = Label(vec_window, text="Y > ", anchor=E, font=("Calibri", 8))
214
    beta_val = Text(vec_window, height=1, borderwidth=0)
215
    beta_val.insert(1.0, str(beta))
216
    gamma_lbl = Label(vec_window, text="Z > ", anchor=E, font=("Calibri", 8))
217
    gamma_val = Text(vec_window, height=1, borderwidth=0)
218
    gamma_val.insert(1.0, str(gamma))
219
    Cosine_lbl.place(x=10, y=120)
220
    alpha_lbl.place(x=25, y=140)
221
    alpha_val.place(x=50, y=141, width=80)
222
    beta_lbl.place(x=25, y=160)
223
    beta_val.place(x=50, y=161, width=80)
224
    gamma_lbl.place(x=25, y=180)
225
    gamma_val.place(x=50, y=181, width=80)
226

227
    vec_window.mainloop()
228
# <----- Single Vector Properties GUI-Backend Code Block End ----->
229

230

231
# <----- GUI Code Block Start ----->
232
# Main Window
233
main = Tk()
234
main.title("Vector Calculator")
235
main.geometry("460x310")
236
main.protocol("WM_DELETE_WINDOW", on_closing)
237

238
# Entry Titles
239
x_lbl = Label(main, text="X", font=("Calibri", 8))
240
y_lbl = Label(main, text="Y", font=("Calibri", 8))
241
z_lbl = Label(main, text="Z", font=("Calibri", 8))
242
x_lbl.place(x=110, y=15)
243
y_lbl.place(x=170, y=15)
244
z_lbl.place(x=230, y=15)
245

246
# Vector 1
247
V1_lbl = Label(main, text="Vector A > ", anchor=E, font=("Calibri", 8))
248
V1_x_val = StringVar()
249
V1_x = Entry(main, textvariable=V1_x_val, font=("Calibri", 8))
250
V1_y_val = StringVar()
251
V1_y = Entry(main, textvariable=V1_y_val, font=("Calibri", 8))
252
V1_z_val = StringVar()
253
V1_z = Entry(main, textvariable=V1_z_val, font=("Calibri", 8))
254
V1_lbl.place(x=20, y=35)
255
V1_x.place(x=90, y=36, width=50)
256
V1_y.place(x=150, y=36, width=50)
257
V1_z.place(x=210, y=36, width=50)
258

259
# Vector 2
260
V2_lbl = Label(main, text="Vector B > ", anchor=E, font=("Calibri", 8))
261
V2_x_val = StringVar()
262
V2_x = Entry(main, textvariable=V2_x_val, font=("Calibri", 8))
263
V2_y_val = StringVar()
264
V2_y = Entry(main, textvariable=V2_y_val, font=("Calibri", 8))
265
V2_z_val = StringVar()
266
V2_z = Entry(main, textvariable=V2_z_val, font=("Calibri", 8))
267
V2_lbl.place(x=20, y=65)
268
V2_x.place(x=90, y=66, width=50)
269
V2_y.place(x=150, y=66, width=50)
270
V2_z.place(x=210, y=66, width=50)
271

272
# Calculate Button
273
Calculate = Button(main, text="Calculate", font=(
274
    "Calibri", 8), command=func_main)
275
Calculate.place(x=270, y=48, width=70)
276

277
# Results Frame ----->
278
frame = Frame(main, bg="#708090")
279
frame.place(x=20, y=90, width=420, height=197)
280

281
# Dot Product
282
dotproduct_lbl = Label(frame, text="Dot Product:", anchor=W,
283
                       bg="#708090", fg="black", font=("Calibri", 8))
284
dotproduct_lbl.place(x=10, y=10)
285
dotproduct_entry_val = StringVar()
286
dotproduct_entry = Entry(
287
    frame, textvariable=dotproduct_entry_val, font=("Calibri", 8))
288
dotproduct_entry.configure(state='readonly')
289
dotproduct_entry.place(x=80, y=11, width=50)
290

291
# Cross Product
292
crossproduct_lbl = Label(frame, text="Cross Product:",
293
                         anchor=W, bg="#708090", fg="black", font=("Calibri", 8))
294
crossproduct_lbl.place(x=10, y=40)
295
crossproduct_x_lbl = Label(
296
    frame, text="X (i) > ", anchor=E, bg="#708090", fg="black", font=("Calibri", 8))
297
crossproduct_y_lbl = Label(
298
    frame, text="Y (j) > ", anchor=E, bg="#708090", fg="black", font=("Calibri", 8))
299
crossproduct_z_lbl = Label(
300
    frame, text="Z (k) > ", anchor=E, bg="#708090", fg="black", font=("Calibri", 8))
301
crossproduct_x_lbl.place(x=30, y=60)
302
crossproduct_y_lbl.place(x=30, y=90)
303
crossproduct_z_lbl.place(x=30, y=120)
304
crossproduct_x_val = StringVar()
305
crossproduct_y_val = StringVar()
306
crossproduct_z_val = StringVar()
307
crossproduct_x = Entry(
308
    frame, textvariable=crossproduct_x_val, font=("Calibri", 8))
309
crossproduct_x.configure(state='readonly')
310
crossproduct_x.place(x=65, y=61, width=50)
311
crossproduct_y = Entry(
312
    frame, textvariable=crossproduct_y_val, font=("Calibri", 8))
313
crossproduct_y.configure(state='readonly')
314
crossproduct_y.place(x=65, y=91, width=50)
315
crossproduct_z = Entry(
316
    frame, textvariable=crossproduct_z_val, font=("Calibri", 8))
317
crossproduct_z.configure(state='readonly')
318
crossproduct_z.place(x=65, y=121, width=50)
319

320
# Angle between both vectors
321
angle_lbl = Label(frame, text="Angle:", anchor=W,
322
                  bg="#708090", fg="black", font=("Calibri", 8))
323
angle_lbl.place(x=10, y=160)
324
angle_val = StringVar()
325
angle = Entry(frame, textvariable=angle_val, font=("Calibri", 8))
326
angle.configure(state='readonly')
327
angle.place(x=50, y=161, width=80)
328

329
# Components
330
component_lbl = Label(frame, text="Components:", anchor=W,
331
                      bg="#708090", fg="black", font=("Calibri", 8))
332
component_lbl.place(x=170, y=10)
333
a_on_b_lbl = Label(frame, text="A on B:", anchor=W,
334
                   bg="#708090", fg="black", font=("Calibri", 8))
335
a_on_b_val = StringVar()
336
a_on_b_ent = Entry(frame, textvariable=a_on_b_val, font=("Calibri", 8))
337
a_on_b_ent.configure(state='readonly')
338
b_on_a_lbl = Label(frame, text="B on A:", anchor=W,
339
                   bg="#708090", fg="black", font=("Calibri", 8))
340
b_on_a_val = StringVar()
341
b_on_a_ent = Entry(frame, textvariable=b_on_a_val, font=("Calibri", 8))
342
b_on_a_ent.configure(state='readonly')
343
a_on_b_lbl.place(x=190, y=30)
344
a_on_b_ent.place(x=230, y=31, width=50)
345
b_on_a_lbl.place(x=190, y=60)
346
b_on_a_ent.place(x=230, y=61, width=50)
347

348
# Projection
349
comp_per_lbl = Label(frame, text="Projection:", anchor=W,
350
                     bg="#708090", fg="black", font=("Calibri", 8))
351
a_on_b_proj_lbl = Label(frame, text="A on B:", anchor=W,
352
                        bg="#708090", fg="black", font=("Calibri", 8))
353
b_on_a_proj_lbl = Label(frame, text="B on A:", anchor=W,
354
                        bg="#708090", fg="black", font=("Calibri", 8))
355
res_x_lbl = Label(frame, text="X", anchor=W, bg="#708090",
356
                  fg="black", font=("Calibri", 8))
357
res_y_lbl = Label(frame, text="Y", anchor=W, bg="#708090",
358
                  fg="black", font=("Calibri", 8))
359
res_z_lbl = Label(frame, text="Z", anchor=W, bg="#708090",
360
                  fg="black", font=("Calibri", 8))
361
comp_per_lbl.place(x=170, y=90)
362
a_on_b_proj_lbl.place(x=190, y=130)
363
b_on_a_proj_lbl.place(x=190, y=160)
364
res_x_lbl.place(x=250, y=110)
365
res_y_lbl.place(x=310, y=110)
366
res_z_lbl.place(x=370, y=110)
367
a_on_b_proj_x_val = StringVar()
368
a_on_b_proj_x = Entry(
369
    frame, textvariable=a_on_b_proj_x_val, font=("Calibri", 8))
370
a_on_b_proj_x.configure(state='readonly')
371
a_on_b_proj_y_val = StringVar()
372
a_on_b_proj_y = Entry(
373
    frame, textvariable=a_on_b_proj_y_val, font=("Calibri", 8))
374
a_on_b_proj_y.configure(state='readonly')
375
a_on_b_proj_z_val = StringVar()
376
a_on_b_proj_z = Entry(
377
    frame, textvariable=a_on_b_proj_z_val, font=("Calibri", 8))
378
a_on_b_proj_z.configure(state='readonly')
379
a_on_b_proj_x.place(x=230, y=131, width=50)
380
a_on_b_proj_y.place(x=290, y=131, width=50)
381
a_on_b_proj_z.place(x=350, y=131, width=50)
382

383
b_on_a_proj_x_val = StringVar()
384
b_on_a_proj_x = Entry(
385
    frame, textvariable=b_on_a_proj_x_val, font=("Calibri", 8))
386
b_on_a_proj_x.configure(state='readonly')
387
b_on_a_proj_y_val = StringVar()
388
b_on_a_proj_y = Entry(
389
    frame, textvariable=b_on_a_proj_y_val, font=("Calibri", 8))
390
b_on_a_proj_y.configure(state='readonly')
391
b_on_a_proj_z_val = StringVar()
392
b_on_a_proj_z = Entry(
393
    frame, textvariable=b_on_a_proj_z_val, font=("Calibri", 8))
394
b_on_a_proj_z.configure(state='readonly')
395
b_on_a_proj_x.place(x=230, y=161, width=50)
396
b_on_a_proj_y.place(x=290, y=161, width=50)
397
b_on_a_proj_z.place(x=350, y=161, width=50)
398

399
# Single vector button entry point
400
Vec_1 = Button(main, text="Vec A Properties", font=(
401
    "Calibri", 9), command=lambda: Show_Vec_Frame(1, values))
402
Vec_2 = Button(main, text="Vec B Properties", font=(
403
    "Calibri", 9), command=lambda: Show_Vec_Frame(2, values))
404

405
# Status Bar
406
Status = Label(main, text="Hello!! :D", fg="green", font=(
407
    "Calibri", 8), bd=1, relief=SUNKEN, anchor=W, padx=3)
408
Status.pack(side=BOTTOM, fill=X)
409

410
main.mainloop()
411
# <----- GUI Code Block End ----->
412

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.