{"id":29868,"date":"2025-07-28T13:27:44","date_gmt":"2025-07-28T11:27:44","guid":{"rendered":"https:\/\/www.valcon.it\/vpython\/?p=29868"},"modified":"2025-09-22T09:00:09","modified_gmt":"2025-09-22T07:00:09","slug":"demo-widgets","status":"publish","type":"post","link":"https:\/\/www.valcon.it\/vpython\/demo-widgets\/","title":{"rendered":"Demo: widgets"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Utilizza <strong>button, checkbox, menu, radio<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom vpython import *\n#----------------------------------------------------------------- Oggetti 3D\nbox_object      = box     (visible=True)\ncone_object     = cone    (visible=False, radius=0.5)\npyramid_object  = pyramid (visible=False)\ncylinder_object = cylinder(visible=False, radius=0.5)\n#----------------------------------------------------------------- Propriet\u00e0 degli oggetti\nscene.width  = 500\nscene.height = 500\nrunning      = True\ncol          = color.cyan\n\ncurrent_object       = box_object\ncurrent_object.color = col\n#----------------------------------------------------------------- Binding\ndef bind_run(evt):\n    global running\n    running = not running\n    \n    if(running): evt.text = &quot;Pause&quot;\n    else:        evt.text = &quot;Run&quot;\n \ndef bind_color_1(evt):\n    global col\n    if(col == color.cyan):                     # cyan --&gt;  red\n        current_object.color = col = color.red\n        evt.text = &quot;&lt;b&gt;Cyan&lt;\/b&gt;&quot;\n        evt.color = color.cyan\n        evt.background = color.red\n        r1.checked = False\n        r2.checked = True\n    else:                                      # red --&gt; cyan\n        current_object.color = col = color.cyan\n        evt.text = &quot;&lt;b&gt;Red&lt;\/b&gt;&quot;\n        evt.color = color.red\n        evt.background = color.cyan\n        r1.checked = True\n        r2.checked = False\n         \ndef bind_color_2(evt):\n    global col\n    if(col == color.cyan):                     # cyan --&gt; red\n        current_object.color = col = color.red\n        cbutton.text = &quot;&lt;b&gt;Cyan&lt;\/b&gt;&quot;\n        cbutton.color = color.cyan\n        cbutton.background = color.red\n    else:                                      # red --&gt; cyan\n        current_object.color = col = color.cyan\n        cbutton.text = &quot;&lt;b&gt;Red&lt;\/b&gt;&quot;\n        cbutton.color = color.red\n        cbutton.background = color.cyan\n \ndef bind_setspeed(evt):\n    wt.text = '{:1.2f}'.format(evt.value)\n \ndef bind_menu(evt):\n    global col, current_object\n    op = current_object.opacity\n    current_axis = current_object.axis\n    current_object.visible = False\n     \n    val = evt.selected\n    if(val == &quot;box&quot;)       : current_object = box_object\n    elif(val == &quot;cone&quot;)    : current_object = cone_object\n    elif(val == &quot;cylinder&quot;): current_object = cylinder_object\n    elif(val == &quot;pyramid&quot;) : current_object = pyramid_object\n     \n    current_object.color   = col\n    current_object.axis    = current_axis\n    current_object.visible = True\n    current_object.opacity = op\n \ndef bind_transparency(evt):\n    if(evt.checked):\n        current_object.opacity = 0.5\n    else:\n        current_object.opacity = 1\n#----------------------------------------------------------------- Interfaccia\nscene.title = &quot;Widgets (button, checkbox, menu, radio, slider)\\n\\n&quot;\nbutton(bind = bind_run,\n       text = &quot;Pause&quot;,\n       pos  = scene.title_anchor)\nscene.append_to_title(' ')\ncbutton = button(background = color.cyan, \n                 bind       = bind_color_1,\n                 color      = color.red,                 \n                 name       = None,\n                 pos        = scene.title_anchor,\n                 text       = '&lt;b&gt;Red&lt;\/b&gt;')\nscene.append_to_title('\\n\\n')\n \nscene.append_to_caption('\\n\\nObject: ')\nmenu(bind    = bind_menu,\n     choices = &#x5B;'box', 'cone', 'cylinder', 'pyramid'],\n     index   = 0)\nscene.append_to_caption('\\n\\nColor: ')\nr1 = radio(bind    = bind_color2,\n           checked = True,\n           name    = 'rads',\n           text    = 'Cyan')\nr2 = radio(bind = bind_color2,\n           name = 'rads',\n           text = 'Red')\nscene.append_to_caption('\\n\\nTransparency: ')\ncheckbox(bind = bind_transparency,\n         text = '')\nscene.append_to_caption(&quot;\\n\\nSpeed: &quot;)   \nsl = slider(bind   = bind_setspeed,\n            max    = 2.0,\n            min    = 0.25,\n            length = 200,\n            right  = 10,\n            value  = 1.0)\nwt = wtext(text = '{:1.2f}'.format(sl.value))\nscene.append_to_caption(' rad\/s')\n#----------------------------------------------------------------- MAIN\ndt = 0.01\nwhile(True):\n    rate(50)\n    if(running):\n        current_object.rotate(angle = sl.value*dt,\n                              axis  = vector(0, 1, 0))\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Utilizza button, checkbox, menu, radio<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-29868","post","type-post","status-publish","format-standard","hentry","category-appl-3d"],"_links":{"self":[{"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/posts\/29868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/comments?post=29868"}],"version-history":[{"count":9,"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/posts\/29868\/revisions"}],"predecessor-version":[{"id":32318,"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/posts\/29868\/revisions\/32318"}],"wp:attachment":[{"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/media?parent=29868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/categories?post=29868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.valcon.it\/vpython\/wp-json\/wp\/v2\/tags?post=29868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}