Updating Labels in VPython Shell

405 views
Skip to first unread message

Tyler M

unread,
Jun 27, 2016, 3:28:57 PM6/27/16
to VPython-users
Hello,

Does anyone know if it is possible to update the text inside of a label in the VPython shell?  I am trying to make a visible counter/timer, and I would prefer it to be in the shell rather than the print statement window.

So far I have tried to set the text equal to the variable:

variable = 0     #Initial value of variable
label(pos=(0,0,0),text = variable)
#Variable then gets updated in a loop

I have also tried to create new labels to go on top of the previous ones as the counter progresses:

condition = false   #Initial condition
label(pos = (0,0,0), text = '0')
#Inside of loop:
    #Update condition
    #if condition = true:
        label(pos = (0,0,0),text='1')
    #if condition = true again:
        label(pos = (0,0,0),text='2')

Please keep in mind I am trying to outline my general methodology for simplification purposes.  This is not my actual code!  So far, neither method has worked.  The first program yields the error message:

ArgumentError: Python argument types in
    None.None(label, int)
did not match C++ signature:
    None(cvisual::label {lvalue}, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >)

I realize the error comes from me not putting text = 'variable', but then it just prints the word variable and no longer a number that I can update.

The second program almost works.  It does print a new label over the old one, but not when I want it to.  Because of this I figured I had made an error in the part of the code I labeled #if condition = true.  Nope.  I tested it, and it works when I create a print statement for the other window, but I want the print statement to be in the same window as the simulation!

I would greatly appreciate it if someone could share their solution, if they have one, to this particular problem.  I hope I have provided enough information.

Thank you!

Bruce Sherwood

unread,
Jun 27, 2016, 4:19:14 PM6/27/16
to VPython-users
It would be very helpful if you would post a simple working code fragment to illustrate your questions, as I'm not sure what your question is.

I will however volunteer that in Classic VPython, which you are using, label text must be a character string, not a numeric value. You can produce a character string using the "format" method for Python strings. For example, the following statement

            label(text="There are {} apples".format(17))

displays "There are 17 apples".

In GlowScript VPython and Jupyter VPython, but not in Classic VPython, the label object detects a numerical value, so that

            label(text=3+5)

display "8".

In all versions of VPython, if you want to change the existing text, do this:

L = label(text="Begin")
.....
L.text = "End"  # this changes the text of the existing label object.

If on the other hand you execute

label(text="Begin")
.....
label(text="End")

you will have created two separate label objects, and you will see a confused image if they have the same position.

Tyler Meehleib

unread,
Jun 27, 2016, 9:37:10 PM6/27/16
to vpytho...@googlegroups.com
My problem is solved.  Thank you very much, sir!

--
You received this message because you are subscribed to a topic in the Google Groups "VPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vpython-users/46WmaSDzjfI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages