如何在tkinter中显示vpython制作的动画?

发布于 2025-01-19 12:21:24 字数 834 浏览 3 评论 0原文

from vpython import *
from tkinter import *

root=Tk()
frame=Frame(root)
frame.pack()
wall=box(pos=vector(0,1,0),size=vector(0.2,3,2),color=color.green)
floor=box(pos=vector(6,-0.6,0),size=vector(14,0.2,4),color=color.green)
Mass=box(pos=vector(12,0,0),velocity=vector(0,0,0),size=vector(1,1,1),mass=1.0,color=color.blue)
pivot=vector(0,0,0)
spring=helix(pos=pivot,axis=Mass.pos-pivot,radius=0.4,constant=1,thickness=0.1,coils=20,color=color.red)
eq=vector(9,0,0)
t=0
dt=0.01
while (t<50):
  rate(100)
  acc=(eq-Mass.pos)*(spring.constant/Mass.mass)
  Mass.velocity=Mass.velocity+acc*dt
  Mass.pos=Mass.pos+Mass.velocity*dt
  spring.axis=Mass.pos-spring.pos
  t=t+dt
root.mainloop()

当我运行此代码时,“ Localhost页面”将弹出以显示动画。但是,我希望在python的root(从tkinter导入的)中显示动画,而不是在其他页面中。我应该怎么办?

from vpython import *
from tkinter import *

root=Tk()
frame=Frame(root)
frame.pack()
wall=box(pos=vector(0,1,0),size=vector(0.2,3,2),color=color.green)
floor=box(pos=vector(6,-0.6,0),size=vector(14,0.2,4),color=color.green)
Mass=box(pos=vector(12,0,0),velocity=vector(0,0,0),size=vector(1,1,1),mass=1.0,color=color.blue)
pivot=vector(0,0,0)
spring=helix(pos=pivot,axis=Mass.pos-pivot,radius=0.4,constant=1,thickness=0.1,coils=20,color=color.red)
eq=vector(9,0,0)
t=0
dt=0.01
while (t<50):
  rate(100)
  acc=(eq-Mass.pos)*(spring.constant/Mass.mass)
  Mass.velocity=Mass.velocity+acc*dt
  Mass.pos=Mass.pos+Mass.velocity*dt
  spring.axis=Mass.pos-spring.pos
  t=t+dt
root.mainloop()

When I run this code, a 'localhost page' will pop out to display the animation. However I wish to display the animation in the root (imported from tkinter) of python instead of in other page. What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2025-01-26 12:21:24

我不认为 vpython 和 tkinter 可以一起工作。有两个问题:1) 3D 显示必须在浏览器窗口中渲染,因为它使用浏览器内置的 WebGL-2 3D 图形库。 2)即使情况并非如此,vpython 的事件处理和 tkinter 的事件处理之间也可能存在冲突。我的猜测是,在 VPython 程序中使用小部件的唯一方法是使用 VPython 小部件。看
Globescript.org/docs/VPythonDocs/controls.html

I don't think that there is a way for vpython and tkinter to work together. There are two issues: 1) The 3D display has to be rendered in a browser window, because it uses the WebGL-2 3D graphics library that is built into the browser. 2) Even if that weren't the case, it's probably the case that there would be conflicts between event handling by vpython and event handling by tkinter. My guess is that the only way to use widgets in a VPython program is to use VPython widgets. See
glowscript.org/docs/VPythonDocs/controls.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文