中学物理模拟实验Python程序集成打包
中学物理模拟实验Python程序集成打包
将多个中学物理模拟实验程序集成到到一个启动界面中,相关介绍&源码可见:https://blog.csdn.net/cnds123/article/details/148844683 ,并用PyInstaller打包使用,希望在没有Python环境的机器上运行,方便使用。
打包成品下载地址见附录。
先看运行效果:
具体过程如下
1.目录结构
2. 创建主启动器界面程序
新建一个文件main_launcher.py,内容如下:
import tkinter as tk
from tkinter import ttk
from 浮力定律模拟 import BuoyancySimulationApp
from 杠杆平衡条件模拟 import LeverSimulator
from 匀速和匀变速直线运动对比 import MotionSimulator
from 凸透镜成像模拟 import ConvexLensSimulator
from 凹透镜成像模拟 import ConcaveLensSimulator
from 抛物运动模拟 import ProjectileMotionApp
from 摩檫力模拟 import FrictionSimulationApp
from 力的合成模拟 import ForceSimulator
from 力的分解模拟 import ForceDecomposer
from 斜面受力分析 import InclinedPlaneSimulator
from 两物体弹性碰撞模拟 import ElasticCollisionAppclass MainLauncher:def __init__(self, root):self.root = rootself.root.title("物理模拟实验")self.root.geometry("400x650")# 设置主界面按钮ttk.Button(root, text="浮力定律模拟", command=lambda: self.open_app(BuoyancySimulationApp)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="杠杆平衡条件模拟", command=lambda: self.open_app(LeverSimulator)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="匀速和匀变速直线运动对比", command=lambda: self.open_app(MotionSimulator)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="凸透镜成像模拟", command=lambda: self.open_app(ConvexLensSimulator)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="凹透镜成像模拟", command=lambda: self.open_app(ConcaveLensSimulator)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="抛物运动模拟", command=lambda: self.open_app(ProjectileMotionApp)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="摩檫力模拟", command=lambda: self.open_app(FrictionSimulationApp)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="力的合成模拟", command=lambda: self.open_app(ForceSimulator)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="力的分解模拟", command=lambda: self.open_app(ForceDecomposer)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="斜面受力分析", command=lambda: self.open_app(InclinedPlaneSimulator)).pack(pady=10, fill=tk.X, padx=50)ttk.Button(root, text="两物体弹性碰撞模拟", command=lambda: self.open_app(ElasticCollisionApp)).pack(pady=10, fill=tk.X, padx=50)# 添加说明按钮ttk.Button(root, text="使用说明", command=self.show_instructions).pack(pady=15, fill=tk.X, padx=50)def open_app(self, AppClass):# 使用 Toplevel 创建子窗口new_window = tk.Toplevel(self.root)app = AppClass(new_window)def show_instructions(self):"""显示使用说明窗口"""# 创建说明窗口instructions_window = tk.Toplevel(self.root)instructions_window.title("使用说明")instructions_window.geometry("620x500")# 添加标题title_label = ttk.Label(instructions_window, text="物理模拟实验室使用说明", font=("Arial", 14, "bold"))title_label.pack(pady=10)# 创建带滚动条的文本框框架frame = ttk.Frame(instructions_window)frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=5)# 创建滚动条scrollbar = ttk.Scrollbar(frame)scrollbar.pack(side=tk.RIGHT, fill=tk.Y)# 创建文本框text_box = tk.Text(frame, wrap=tk.WORD, yscrollcommand=scrollbar.set,font=("Arial", 11), padx=10, pady=10)text_box.pack(fill=tk.BOTH, expand=True)# 配置滚动条scrollbar.config(command=text_box.yview)# 添加说明内容instructions = """======== 物理模拟实验使用说明 ========本软件包含多个物理实验模拟模块,帮助您直观理解各种物理现象。设计:WKJ版本号: 1.01特别提示,相关知识&源码:https://blog.csdn.net/cnds123/article/details/148844683使用提示:- 点击相应按钮打开物理模拟模块- 每个模块都有参数调整控件和可视化展示- 部分模块包含动画演示功能- 计算结果会实时更新显示注意:
理实验模拟程序和实体实验两者是共生协同的关系,物理实验模拟程序绝非对实体实验的简单替代。
模拟程序中点击鼠标调整参数的动作,无法替代真实接触身临其境带来的现实反馈——耐心细致的动手能力、环境影响误差处理、物理直觉判断处置等体验。"""# 插入文本并设置为只读text_box.insert(tk.END, instructions)text_box.config(state=tk.DISABLED) # 设置为只读# 添加关闭按钮close_button = ttk.Button(instructions_window, text="关闭", command=instructions_window.destroy)close_button.pack(pady=10)if __name__ == "__main__":root = tk.Tk()app = MainLauncher(root)root.mainloop()
3. 问题处理
a.点击“杠杆平衡条件模拟”按钮报错:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Python\Python312\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\Wang\Desktop\新建文件夹 (12)\集成打包2\中学物理实验模拟程序启动.py", line 23, in <lambda>
command=lambda: self.open_app(LeverSimulator)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Wang\Desktop\新建文件夹 (12)\集成打包2\中学物理实验模拟程序启动.py", line 29, in open_app
app = AppClass(new_window)
^^^^^^^^^^^^^^^^^^^^
TypeError: LeverSimulator.__init__() takes 1 positional argument but 2 were given
解决方案:
修改杠杆平衡条件模拟.py中的LeverSimulator类,使其接受一个可选的master参数:
class LeverSimulator:
def __init__(self, master=None): # 添加 master 参数
if master:
self.root = master
else:
self.root = tk.Tk()
self.root.title("杠杆原理模拟器 - 三类杠杆演示")
self.root.geometry("1200x960")
self.root.configure(bg='lightblue')
# 其余代码保持不变...
self.canvas_width = 1100
self.canvas_height = 400
# ... 其他初始化代码 ...
匀速和匀变速直线运动对比,也需要类似处理。
b.力的分解界面变小了,而单独运行力的分解界面是正常的,如何处理?
解决方案:修改力的分解模拟程序,添加最小尺寸限制:
class ForceDecomposer:
def __init__(self, root):
self.root = root
self.root.title("力的分解模拟器")
self.root.minsize(800, 500) # 设置最小尺寸
# 其余代码保持不变...
4.使用 PyInstaller 打包
pyinstaller --onefile --windowed --name "物理模拟实验室" main_launcher.py
附录
打包成品,在win10可用!可见:https://download.csdn.net/download/cnds123/91336927