Text Input

The commands winput and input allow a user to type input to be processed by a program.

input

The input() function accepts user input from a pop-up dialog box. This is similar to the Python input() function, except that it allows the specification of a prompt.

mytext = input(prompt='Enter a color:')
Parameters:

prompt (string) – Text of the prompt.

If the user presses ‘Cancel’ instead of typing a string, the function returns None. Input can be converted to a number: val = float(mytext).

ss = input( 'What is your name?')

if ss != None:
        print('Hello',  ss)
else:
        print('Goodbye')

winput

The winput() widget can be placed either in the caption area of a canvas (the default position) or in the title area. It displays a box into which a user can type. The user must select the text input area before typing. A function bound to a winput widget is executed if the user presses Enter or Tab or clicks somewhere other than in a VPython canvas. The attributes below are labeled “not modifiable” if they cannot be modified after creation.

ww = winput(bind=myfunction, prompt='value', type='numeric')
Parameters:
  • bind (function) – The function to be called when the input is ended (by Enter, Tab, or click outside canvas).

  • prompt (string) – Text displayed to left of the winput box. (WebVPython only.)

  • type (string) – The type of input requested: ‘numeric’ (default) or ‘string’. Not modifiable.

  • pos (attribute of canvas) – Location of checkbox. Default is scene.caption_anchor.

  • width (scalar) – Width of input box in pixels. Default is 100. Not modifiable.

  • height (scalar) – Height of input box in pixels. Default 20. Not modifiable.

  • text (string) – The text displayed in the box, whether entered by user or set by program.

  • number (scalar) – The number that results from evaluating the user’s input text if the type is ‘numeric’, otherwise None. Not modifiable.

  • disabled (boolean) – If True, text is grayed out and winput is inactive.

  • delete() (method) – A method to delete the winput box from the displayl.

This code uses winput to adjust the radius of a sphere:

ss = sphere(pos=vec(-2,0,0), radius=0.2, color=color.yellow)

def change_radius(evt):
        rad = evt.number
        if rad != None:
                if rad < 10:
                        ss.radius=evt.number
                else:
                        print(rad + ' is too large')
                        ww.text = ''

scene.append_to_caption('Radius must be less than 10 \n')
ww = winput(prompt='Radius:', bind=change_radius, type='numeric')