vpython / vpython-jupyter

3D visualization made easy
MIT License
138 stars 64 forks source link

Adding options to the menu widget #175

Closed Johnny-J3 closed 2 years ago

Johnny-J3 commented 2 years ago

Hi, I am relatively new to vpython and am working on a project where I would benefit from being able to add options to an existing menu widget. I see that in the documentation it says that the menu choices could be updated by specifying a new list of strings. I was able to get my new choices to appear on the menu in my browser, but it appears like this breaks the indexing system on the menu. This is demonstrated by the example program below.

from vpython import *

scene = canvas()
scene.append_to_title("<h1>Bug Demonstration</h1>")
scene.append_to_title("\n")

def menuFunc(menu):
    print("Selected: " + menu.selected)
    print("Index: " + str(menu.index))

options = 3
menuChoices = ["Choice 1", "Choice 2", "Choice 3"]
m = menu(pos = scene.title_anchor, choices = menuChoices, bind = menuFunc)
scene.append_to_title("  ")

def addChoice():
    global options, menuChouces, m
    options += 1
    menuChoices.append("Choice " + str(options))
    m.choices = menuChoices

b = button(pos = scene.title_anchor, bind = addChoice, text = "Add Menu Choice")

box(pos = vector(0,0,0))

I have the menu binded to a function that will print the menu selection and index. This works properly before I add more choices, but after the choices are added, the function will only print the index and selection of the last index when any choice in the menu is selected besides the first one. I have tried to fix this problem in my main project, but all solutions that I have come up with have caused the same issue.

I am using vpython version 7.6.2, python version 3.8.3, running the program in spyder, and using chrome to view the canvas.

Johnny-J3 commented 2 years ago

Found a small typo in my example program. Here is the updated version:

from vpython import *

scene = canvas()
scene.append_to_title("<h1>Bug Demonstration</h1>")
scene.append_to_title("\n")

def menuFunc(menu):
    print("Selected: " + menu.selected)
    print("Index: " + str(menu.index))

options = 3
menuChoices = ["Choice 1", "Choice 2", "Choice 3"]
m = menu(pos = scene.title_anchor, choices = menuChoices, bind = menuFunc)
scene.append_to_title("  ")

def addChoice():
    global options, menuChoices, m
    options += 1
    menuChoices.append("Choice " + str(options))
    m.choices = menuChoices

b = button(pos = scene.title_anchor, bind = addChoice, text = "Add Menu Choice")

box(pos = vector(0,0,0))
BruceSherwood commented 2 years ago

It would be very helpful if you would list in order exactly what to click to see the problem. Thanks.

Johnny-J3 commented 2 years ago

Thank you for your reply. Here are the steps to demonstrate the issue:

  1. First, select different choices on the menu and check to see what the console printed. It should print out the correct choice that you have selected.
  2. Click on the add menu choice button. This should add the fourth choice to the menu.
  3. Now try changing choices on the menu again. You should see that when you have selected choices 2 or 3, it prints that you have choice 4 selected (That's what happens on my computer).

I've taken this to mean that once the number of choices on a predefined menu is changed, the menu widget can no longer properly recognize which choice is selected.

Thank you for looking into this.

BruceSherwood commented 2 years ago

Thanks much for the clear instructions. I've found the needed fix, which will be available the next time there's an update to VPython 7.

Bruce

On Fri, Oct 15, 2021 at 4:51 AM Johnny-J3 @.***> wrote:

Thank you for your reply. Here are the steps to demonstrate the issue:

  1. First, select different choices on the menu and check to see what the console printed. It should print out the correct choice that you have selected.
  2. Click on the add menu choice button. This should add the fourth choice to the menu.
  3. Now try changing choices on the menu again. You should see that when you have selected choices 2 or 3, it prints that you have choice 4 selected (That's what happens on my computer).

I've taken this to mean that once the number of choices on a predefined menu is changed, the menu widget can no longer properly recognize which choice is selected.

Thank you for looking into this.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vpython/vpython-jupyter/issues/175#issuecomment-944235745, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAITILYIX3KYEWACSAOV2CTUHAIVTANCNFSM5F75ITDQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

BruceSherwood commented 2 years ago

If you'd like to get the fix now, do the following. In site-packages/vpython/vpython_libraries, in modify glowcomm.js and glowcomm.html. Search for "if (attr == 'choices')". Replace the statements in this if condition with this single statement:

val = m[3].slice(2,-2).split("', '") // choices separated by ', '