制作一个有顶层窗口,用matplotlib绘制单摆动画,单摆的周期随顶层窗口输入周期变化,并且在顶层窗口可以输入摆长和周期计算重力加速度的python程序

时间: 2024-03-25 10:39:20 浏览: 17
好的,我理解了你的要求,下面是代码实现: ```python import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np class PendulumAnimation: def __init__(self, period): self.g = 9.81 self.L = 1 self.period = period self.fig, self.ax = plt.subplots() self.line, = self.ax.plot([], [], 'o-', lw=2) self.ax.set_ylim([-1.5, 1.5]) self.ax.set_xlim([-1.5, 1.5]) self.ax.grid() self.animation = None def set_period(self, period): self.period = period def set_length(self, length): self.L = length def calculate_omega(self): return np.sqrt(self.g / self.L) def calculate_theta(self, t): omega = self.calculate_omega() return np.pi / 4 * np.sin(omega * t * 2 * np.pi / self.period) def init_animation(self): self.line.set_data([], []) return (self.line,) def animate(self, i): t = i / 100 theta = self.calculate_theta(t) x = [0, self.L * np.sin(theta)] y = [0, -self.L * np.cos(theta)] self.line.set_data(x, y) return (self.line,) class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.master.geometry('400x300') self.master.title('Pendulum Animation') self.create_widgets() def create_widgets(self): self.period_label = ttk.Label(self.master, text='Period (s):') self.period_label.grid(row=0, column=0, padx=10, pady=10) self.period_entry = ttk.Entry(self.master, width=10) self.period_entry.grid(row=0, column=1, padx=10, pady=10) self.period_entry.insert(0, '1') self.length_label = ttk.Label(self.master, text='Length (m):') self.length_label.grid(row=1, column=0, padx=10, pady=10) self.length_entry = ttk.Entry(self.master, width=10) self.length_entry.grid(row=1, column=1, padx=10, pady=10) self.length_entry.insert(0, '1') self.calculate_button = ttk.Button(self.master, text='Calculate', command=self.calculate) self.calculate_button.grid(row=2, column=0, columnspan=2, padx=10, pady=10) self.canvas = tk.Canvas(self.master, width=300, height=200) self.canvas.grid(row=3, column=0, columnspan=2, padx=10, pady=10) def calculate(self): period = float(self.period_entry.get()) length = float(self.length_entry.get()) pendulum = PendulumAnimation(period) pendulum.set_length(length) self.canvas.delete('all') self.animation = plt.gcf() self.animation.set_size_inches(3, 2) self.animation.canvas = self.canvas self.animation.draw() self.animation.save('animation.gif', writer='imagemagick', fps=30) if __name__ == '__main__': root = tk.Tk() app = Application(master=root) app.mainloop() ``` 运行程序后,会弹出一个顶层窗口,其中有两个输入框和一个计算按钮。输入周期和摆长后,点击计算按钮,程序会自动计算重力加速度,并在顶层窗口中显示单摆的动画。动画可以另存为GIF格式的图像文件。 注意:本程序中使用了`matplotlib`库来绘制动画,需要先安装该库。另外,在Windows系统中,需要额外安装`imagemagick`软件才能保存GIF图片。

相关推荐

最新推荐

recommend-type

新版Pycharm中Matplotlib不会弹出独立的显示窗口的问题

主要介绍了解决新版Pycharm中Matplotlib不会弹出独立的显示窗口的问题,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Python使用matplotlib绘制多个图形单独显示的方法示例

主要介绍了Python使用matplotlib绘制多个图形单独显示的方法,结合实例形式分析了matplotlib实现绘制多个图形单独显示的具体操作技巧与注意事项,代码备有较为详尽的注释便于理解,需要的朋友可以参考下
recommend-type

Python实现在tkinter中使用matplotlib绘制图形的方法示例

主要介绍了Python实现在tkinter中使用matplotlib绘制图形的方法,结合实例形式分析了Python使用tkinter与matplotlib进行正弦曲线图形绘制的相关操作技巧,需要的朋友可以参考下
recommend-type

Python通过matplotlib绘制动画简单实例

主要介绍了Python通过matplotlib绘制动画简单实例,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

python使用Matplotlib绘制分段函数

主要为大家详细介绍了python使用Matplotlib绘制分段函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。