#При нажатии на кнопку программа должна вывести движение маятника в графическом интерфейсе, но этого не происходит, помогите советом#
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
graph_window = None
graph_window = plt.gcf().canvas.manager.window
class Pendulum:
def __init__(self, length, amplitude, mass, friction, pivot, gravity):
self.length = length
self.amplitude = amplitude
self.mass = mass
self.friction = friction
self.pivot = pivot
self.gravity = gravity
self.time = np.linspace(0, 10, 1000) # Time points for animation
self.animation = None
def calculate_trajectory(self):
theta = self.amplitude * np.cos(np.sqrt(self.gravity / self.length) * self.time)
return theta
def animate(self):
fig, ax = plt.subplots()
ax.set_aspect('equal', 'box')
ax.set_xlim(-self.length - 1, self.length + 1)
ax.set_ylim(-self.length - 1, self.length + 1)
line, = ax.plot([], [], 'o-', lw=2)
x_axis, = ax.plot([], [], 'k--', lw=1)
y_axis, = ax.plot([], [], 'k--', lw=1)
support_line, = ax.plot([], [], 'ro', markersize=5)
def update(frame):
x = [self.pivot[0], self.pivot[0] + self.length * np.sin(frame)]
y = [self.pivot[1], self.pivot[1] - self.length * np.cos(frame)]
line.set_data(x, y)
x_axis.set_data([-self.length - 1, self.length + 1], [0, 0])
y_axis.set_data([0, 0], [-self.length - 1, self.length + 1])
support_x = [self.pivot[0]]
support_y = [self.pivot[1]]
support_line.set_data(support_x, support_y)
return line, x_axis, y_axis, support_line
animation = FuncAnimation(fig, update, frames=self.calculate_trajectory(), interval=10, blit=True)
self.animation = animation
plt.show()
def update_pendulum():
length = float(length_entry.get())
amplitude = float(amplitude_entry.get())
mass = float(mass_entry.get())
friction = float(friction_entry.get())
pivot_x = float(pivot_x_entry.get())
pivot_y = float(pivot_y_entry.get())
gravity = float(gravity_entry.get())
pendulum.length = length
pendulum.amplitude = amplitude
pendulum.mass = mass
pendulum.friction = friction
pendulum.pivot = (pivot_x, pivot_y)
pendulum.gravity = gravity
pendulum.animate()
def on_close():
global graph_window
if graph_window is not None:
graph_window.destroy()
root.destroy()
# Create an instance of the Pendulum class
pendulum = Pendulum(1.0, 0.5, 1.0, 0.1, (0, 0), 9.81)
# Create the Tkinter GUI
root = tk.Tk()
root.title("Pendulum Animation")
# Create the figure for plotting
fig = Figure(figsize=(5, 5), dpi=100)
ax = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# Create the input fields for the variables
length_label = tk.Label(root, text="Length:")
length_label.pack()
length_entry = tk.Entry(root)
length_entry.pack()
amplitude_label = tk.Label(root, text="Amplitude:")
amplitude_label.pack()
amplitude_entry = tk.Entry(root)
amplitude_entry.pack()
mass_label = tk.Label(root, text="Mass:")
mass_label.pack()
mass_entry = tk.Entry(root)
mass_entry.pack()
friction_label = tk.Label(root, text="Friction:")
friction_label.pack()
friction_entry = tk.Entry(root)
friction_entry.pack()
pivot_x_label = tk.Label(root, text="Pivot X:")
pivot_x_label.pack()
pivot_x_entry = tk.Entry(root)
pivot_x_entry.pack()
pivot_y_label = tk.Label(root, text="Pivot Y:")
pivot_y_label.pack()
pivot_y_entry = tk.Entry(root)
pivot_y_entry.pack()
gravity_label = tk.Label(root, text="Gravity:")
gravity_label.pack()
gravity_entry = tk.Entry(root)
gravity_entry.pack()
update_button = tk.Button(root, text="Update Pendulum", command=update_pendulum)
update_button.pack()
root.protocol("WM_DELETE_WINDOW", on_close)
# Run the Tkinter event loop
root.mainloop()