|
楼主 |
发表于 2024-5-19 22:33:27
|
显示全部楼层
from sympy import symbols, Eq, solve, nsolve
import math
import turtle as t
import tkinter as tk
def on_submit_button_click():
global L1, L2, L3, L4,speed_angle1 # 声明变量是全局变量
L1_value = L1_entry.get()
L2_value = L2_entry.get()
L3_value = L3_entry.get()
L4_value = L4_entry.get()
speed_angle1_value = speed_angle1_entry.get()
root = tk.Tk()
root.title("变量赋值界面")
# 创建Entry控件和标签
labels = ['L1', 'L2', 'L3', 'L4', 'speed_angle1 ']
entries = []
for label in labels:
tk.Label(root, text=label + ":").pack()
entry = tk.Entry(root)
entry.pack()
entries.append(entry)
L1_entry, L2_entry, L3_entry, L4_entry, speed_angle1_entry= entries
submit_button = tk.Button(root, text="提交", command=on_submit_button_click)
submit_button.pack()
# 开始Tkinter事件循环
root.mainloop()
#L1,L2,L3,L4 = 26.5,111.6,67.5,87.5
#角位移
angle1 = []
angle2 = []
angle3 = []
for i in range(120):#每3度求解存储数据
angle1.append(i*3)
for constant in angle1:
#解非线性方程,求解角位移
constant_c = L4-L1*math.cos(math.radians(constant))
constant_s = -L1*math.sin(math.radians(constant))
w, x, y, z = symbols('w x y z ')
eqs = [Eq(L2*w - L3*x, constant_c),
Eq(L2*y - L3*z, constant_s),
Eq(w**2 + y**2,1),
Eq(x**2 + z**2,1)]
displacement_quation = solve(eqs, [w, x, y, z])
#判断符合实际的解
if displacement_quation[0][3]>= 0:
angle2.append(math.acos(displacement_quation[0][0]))
angle3.append(math.acos(displacement_quation[0][1]))
else:
angle2.append(math.acos(displacement_quation[1][0]))
angle3.append(math.acos(displacement_quation[1][1]))
#角速度
speed_angle1 = 1 #rad/s
speed_angle2 = []
speed_angle3 = []
for i in range(len(angle1)):
constant2_s = L2*math.sin(angle2[i])
constant3_s = L3*math.sin(angle3[i])
constant1_s = L1*math.sin(math.radians(angle1[i]))*speed_angle1
constant2_c = L2*math.cos(angle2[i])
constant3_c = L3*math.cos(angle3[i])
constant1_c = L1*math.cos(math.radians(angle1[i]))*speed_angle1
x, y = symbols('x y')
eqs = [Eq(-constant2_s*x + constant3_s*y, constant1_s),
Eq(constant2_c*x - constant3_c*y, -constant1_c)]
speed_quation = solve(eqs, [x, y])
speed_angle2 .append(speed_quation[x])
speed_angle3 .append(speed_quation[y])
#角加速度
acceleration_angle1 = 0
acceleration_angle2 = []
acceleration_angle3 = []
for i in range(len(angle1)):
constant2_s = L2*math.sin(angle2[i])
constant3_s = L3*math.sin(angle3[i])
constant1_s = L1*math.sin(math.radians(angle1[i]))
constant2_c = L2*math.cos(angle2[i])
constant3_c = L3*math.cos(angle3[i])
constant1_c = L1*math.cos(math.radians(angle1[i]))
constant_s = constant2_s*speed_angle2[i]**2 - constant3_s*speed_angle3[i]**2 + constant1_s
constant_c = constant2_c*speed_angle2[i]**2 - constant3_c*speed_angle3[i]**2 + constant1_c
x, y = symbols('x y')
eqs = [Eq(-constant2_s*x + constant3_s*y,constant_c),
Eq(constant2_c*x - constant3_c*y,constant_s)]
acceleration_quation = solve(eqs, [x, y])
acceleration_angle2 .append(acceleration_quation[x])
acceleration_angle3 .append(acceleration_quation[y])
t.hideturtle()
t.speed("fastest")
# 直角坐标系
t.color('red')
t.penup()
t.goto(-500, 0)
t.pendown()
t.goto(500, 0)
t.penup()
t.goto(0, -500)
t.pendown()
t.goto(0, 500)
t.penup()
t.goto(5*angle1[0], 5*math.degrees(angle2[0]))
t.color('blue')
t.width(2)
t.pendown()
for i in range(len(angle1)):
t.goto(5*angle1[i],5*math.degrees(angle2[i]))
t.penup()
t.goto(5*angle1[0], 5*math.degrees(speed_angle2[0]))
t.color('blue')
t.width(3)
t.pendown()
for i in range(len(angle1)):
t.goto(5*angle1[i], 5*math.degrees(speed_angle2[i]))
t.penup()
t.goto(5*angle1[0],5*math.degrees(acceleration_angle2[0]))
t.color('blue')
t.width(4)
t.pendown()
for i in range(len(angle1)):
t.goto(angle1[i], math.degrees(acceleration_angle2[i]))
t.penup()
t.goto(5*angle1[0], 5*math.degrees(angle3[0]))
t.color('yellow')
t.width(2)
t.pendown()
for i in range(len(angle1)):
t.goto(5*angle1[i],5*math.degrees(angle3[i]))
t.penup()
t.goto(5 * angle1[0], 5 * math.degrees(speed_angle3[0]))
t.color('yellow')
t.width(3)
t.pendown()
for i in range(len(angle1)):
t.goto(angle1[i], math.degrees(speed_angle3[i]))
t.penup()
t.goto(5*angle1[0],5* math.degrees(acceleration_angle3[0]))
t.color('yellow')
t.width(4)
t.pendown()
for i in range(len(angle1)):
t.goto(5 * angle1[i], 5 * math.degrees(acceleration_angle3[i]))
t.mainloop()
赋值界面有错误 |
|