简体   繁体   English

将按钮绑定到VPython 7函数

[英]Binding a button to a VPython 7 function

I am currently trying to make a physics simulation using VPython on my laptop. 我目前正在尝试在笔记本电脑上使用VPython进行物理模拟。 I want to make a GUI so that when the client presses the run simulation button, it opens chrome and the simulation runs, after taking the user's input from entry boxes. 我想制作一个GUI,以便当客户端按下运行模拟按钮时,它会在从输入框中获取用户输入后打开chrome并运行模拟。 However, when I run the source code, chrome automatically opens and I want it to open only when the user presses the run simulation button. 但是,当我运行源代码时,chrome自动打开,并且我希望仅在用户按下运行模拟按钮时打开它。 I tried using the lambda method to bind the function to the button, but it isn't working. 我尝试使用lambda方法将功能绑定到按钮,但无法正常工作。 Here is a very simplified version of what I am trying to do: 这是我正在尝试做的一个非常简化的版本:

import tkinter as tk
from vpython import *

### Simulation ###
def run_simulation(r):
    sphere(radius=r)

### GUI ###
root = tk.Tk()
text_variable = tk.StringVar()
entry = tk.Entry(root, textvariable=text_variable, width=10)
entry.pack()
button = tk.Button(root, text='Run Simulation', command= lambda: run_simulation(eval(text_variable.get())))
button.pack()
root.mainloop()

In short, if you run this code, it will open chrome automatically (even though I used lambda), but I only want it to do so if the button is clicked. 简而言之,如果您运行此代码,它将自动打开chrome(即使我使用了lambda),但我只希望在单击按钮时这样做。 Thanks in advance. 提前致谢。

Here are a bunch of vpython demo programs that you can run in a browser using glowscript vpython 这是一堆vpython演示程序,您可以使用glowscript vpython在浏览器中运行

http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/ http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/

Just click run beside the image and it runs the simulation. 只需单击图像旁边的运行,它将运行模拟。

These same vpthon Demo programs are also found on github here. 这些相同的vpthon Demo程序也可以在github上找到。

https://github.com/BruceSherwood/vpython-jupyter/tree/master/Demos_no_notebook https://github.com/BruceSherwood/vpython-jupyter/tree/master/Demos_no_notebook

you can run the programs from the Windows Command prompt if you have python 3.6 installed on your machine. 如果您的计算机上安装了python 3.6,则可以从Windows命令提示符运行程序。 Just type 只需输入

python BinaryStar.py python BinaryStar.py

and it will open a web browser and run the BinaryStar vpython demo program. 它将打开一个网络浏览器并运行BinaryStar vpython演示程序。 You can do the same for the other demo programs in the above directory on github. 您可以在github上的上述目录中对其他演示程序执行相同的操作。

Maybe you can get your button to run the command "python myVpythonProgram.py" to run the vpython program you want to run in a browser. 也许您可以使按钮运行命令“ python myVpythonProgram.py”来运行要在浏览器中运行的vpython程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM