简体   繁体   中英

Binding a button to a VPython 7 function

I am currently trying to make a physics simulation using VPython on my laptop. 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. 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. I tried using the lambda method to bind the function to the button, but it isn't working. 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. Thanks in advance.

Here are a bunch of vpython demo programs that you can run in a browser using glowscript vpython

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.

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. Just type

python BinaryStar.py

and it will open a web browser and run the BinaryStar vpython demo program. You can do the same for the other demo programs in the above directory on 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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