Hi I've recently downloaded Vpython and python3.1
I have been impressed with the 3D animation ability of Vpython and playing around with the sample code available. I am a physics teacher and would like to make some interactive simulations to use in class. Is there any way that Vpython and a GUI package like tkinter can marry together such that a Vpython window can be place in a canvas/frame or something of a tkinter GUI.

I have written stuff for my school before but with C# using visual studio 2008.

I love the idea of learning python but think I may struggle with the lack on intellisens (that I depend on in visual studio) in IDLE. I think I'll need to really learn code for once.

WOuld appreciate any advice.
Dale

Recommended Answers

All 4 Replies

Yes, you can do it with the help of Python module thread ...

# using VPython (visual) and Tkinter together
# with the help of Python module thread
# tested with VPython 5.4.1 and Python 2.7.1 by vegaseat

import visual as vs
import Tkinter as tk
import thread

# will be global
sphere = None

def vthread():
    global sphere
    vs.scene.title = "Sphere in space (3D drag with right mouse button)"
    vs.scene.autoscale = False
    sphere = vs.sphere(pos=(0, 0, 0), color=vs.color.green)

def move_sphere_incr_x(event=None):
    """moves along the x axis incrementing x"""
    x, y, z = sphere.pos
    sphere.pos = (x+1, y, z)

def move_sphere_decr_x(event=None):
    """moves along the x axis decrementing x"""
    x, y, z = sphere.pos
    sphere.pos = (x-1, y, z)

root = tk.Tk()
w = 300
h = 200
x = 450
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
root.title("Control Sphere from here")

b_incr_x = tk.Button(root, text="move on x axis increment x")
# bind passes an event to function
b_incr_x.bind("<Button-1>", move_sphere_incr_x)
b_incr_x.grid(row=0, column=0, padx=20, pady=10)

b_decr_x = tk.Button(root, text="move on x axis decrement x")
# bind passes an event to function
b_decr_x.bind("<Button-1>", move_sphere_decr_x)
b_decr_x.grid(row=1, column=0, padx=10)

# use thread to do run VPython and Tkinter simultaneously
# thread.start_new_thread(function, args)
# args is an empty tuple here
sphere = thread.start_new_thread(vthread, ())

root.mainloop()

You can add buttons and functions to change y and z too.

You can get VPython for Python27 from:
http://vpython.org/contents/download/VPython-Win-Py2.7-5.41.exe

commented: interesting concept +12

Great, I'm glad it's doable.
Doesn't seem to work with python 3, I'll check out how threads behave differently in python 3 and try and adapt it.

thanks again
Dale.

I used Python27 because I couldn't find vpython for Python32 yet. Even though it is available for Python31.

Have you made it work in python3? I am very eager to know the method. I will appreciate it if you reply to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.