Multiple Windows

57 views
Skip to first unread message

Brad Strassburger

unread,
Mar 11, 2016, 3:05:25 PM3/11/16
to VPython-users
I was wondering if there is anyway to run multiple windows. I would like to, for example, have a splash screen in a window, and then have all of that material disappear and a clean window start. I was thinking the best way of doing this is to literally close the first window and open a new one. Is anything like this possible?

Bruce Sherwood

unread,
Mar 11, 2016, 6:55:03 PM3/11/16
to VPython-users
It is easy to run multiple windows. See the VPython Help on "displays" (or in GlowScript and Jupyter versions, "canvases"). By default VPython creates a display named "scene", but you can make another named "d2" by executing "d2 = display()", and see the Help for options for this second display.

However, the situation you describe can be handled much more simply:

from visual import *
L = label(text="Click to proceed")
scene.waitfor("click")
L.visible = False
.....

Brad Strassburger

unread,
Mar 12, 2016, 4:09:56 PM3/12/16
to VPython-users
So I have been trying to put together a really really simple example to get the ball rolling, but seem to be running into a lot of errors. When I comment out the 'while' statement in the functionWelcome it works in terms of displaying the text but will not accept the key event. And when I use the sleep event in the other two functions it will wait the correct amount of time but the screen blanks out. Are there any obvious things that I might be missing?

from visual import *
import time

def function1():
    Bob = sphere(pos=vector(-10,-10,0), radius = 1, color = color.red)
    Bill = sphere(pos=vector(10,10,0), radius = 1.5, color = color.blue)
    Bob.visible = True
    Bill.visible = True   
    time.sleep(10)
    Bill.visible = False
    Bob.visible = False
    functionWelcome()
    
def function2():
    Steve = sphere(pos=vector(10,-10,0), radius = 0.5, color = color.cyan)
    Morry = sphere(pos=vector(-10,10,0), radius = 1.7, color = color.yellow)
    time.sleep(10)
    Steve.visible = False
    Morry.visible = False
    functionWelcome()

def functionWelcome():
    Hello = text(text = "Press h or g to start", align = "center", depth = 0.3, color=color.green)
    while true:
        if scene.kb.keys:
            key = scene.kb.getkey()
            if key == "h":
                Hello.visible = False
                function1()
            elif hey == "g":
                Hello.visible = False
                function2()


functionWelcome()

Bruce Sherwood

unread,
Mar 12, 2016, 4:24:29 PM3/12/16
to VPython-users
You need a rate statement in the while loop; otherwise the program locks up: rate(30) is appropriate. Without the rate statement no updates to the screen can occur.

There is a typo: elif hey == "g": should be elif key == "g":

Do not use time.sleep(); use sleep() instead, a VPython function. Again, the problem is that time.sleep() doesn't know anything about graphics updates and consequently prevents them.

Brad Strassburger

unread,
Mar 12, 2016, 4:33:15 PM3/12/16
to VPython-users
Perfect. After all of this time, I am still making simple mistakes. So I guess the last part of my question would be how to close a new window (i.e. I create a second window and something happens, then the window closes). I have tried exit, but it just shuts down the entire program.

Bruce Sherwood

unread,
Mar 12, 2016, 4:40:56 PM3/12/16
to VPython-users
See the VPython documentation on "displays", particularly the exit and delete options.

Brad Strassburger

unread,
Mar 12, 2016, 4:55:59 PM3/12/16
to VPython-users
Alright, so using delete seems to minimize the window but I was hoping to fully close the window, but c'est las vie. The last issue that I am having is that when I create a new scene, it does not seem to allow for input.

def functionWelcome():
    sceneOpen = display(title='Welcome to My Game', x=0, y=0, width=400, height=400, center=(0,0,0), background=(0,0,0))
    while true:
        rate(30)
        Hello = text(text = "Press h or g to start\ne to exit", align = "center", depth = 0.3, color=color.green)
        if scene.kb.keys:
            key = scene.kb.getkey()
            if key == "h":
                sceneOpen.delete()
                function1()
            elif key == "g":
                sceneOpen.delete()
                function2()
            elif key == "e":
                exit()


functionWelcome()

Bruce Sherwood

unread,
Mar 12, 2016, 6:14:44 PM3/12/16
to VPython-users
It's puzzling that you say that disp.delete() minimizes the window. What platform do you use, Windows/Mac/Linux?

You create a display named sceneOpen but then you look for events happening in the default display named scene, so no events happen, the scene display never having been activated.

It would make more sense to create the 3D text before entering the while loop than to recreate it 30 times per second.

Brad Strassburger

unread,
Mar 13, 2016, 11:55:58 AM3/13/16
to VPython-users
Bruce, thank you so much for your help and your patience. Unfortunately I do not fully understand what you meant in terms of your last post. First off, I am using Mac which may be why it is minimizing instead of closing. Not a big deal at this point. Where I am confused, I guess, is in the creating scenes. You said that I create a new scene called sceneOpen but then I am looking for events in the main scene, so perhaps I am confused with what scenes actually do. I assumed that when I created the new scene in a function, that all of the elements in that function would be in that scene also. So obviously I am missing a step somewhere could you possibly lead me in the correct direction as I am admittedly stumped.

Bruce Sherwood

unread,
Mar 13, 2016, 3:26:09 PM3/13/16
to VPython-users
A VPython "display" is a window in which objects such as boxes or spheres are displayed. When you start up VPython, it executes "scene = display()" for you, and if you then say "box()" a window is created (the display is "activated" now that there is something to show in it) and the box is displayed in that window.

If you say "sceneOpen = display" and then say "box()" a window is created and a box shown in that window, and the display named "scene" is not activated; only one window exists. Events are associated with displays. For example, scene.mouse.pos is the position of the mouse in a display named "scene", whereas sceneOpen.mouse.pos is the position of the mouse in a display named "sceneOpen".

It is very strange that deleting a display on a Mac is equivalent to setting the display's visible attribute to False. I was not aware of this bug.

Brad Strassburger

unread,
Mar 13, 2016, 3:47:22 PM3/13/16
to VPython-users
I finally see my error. I was using the key events for "scene" when I should have used sceneOpen. It's always in the details.

Thanks so much for helping me out.
Reply all
Reply to author
Forward
0 new messages