Slider的标题不可见 (VPython)

huangapple go评论104阅读模式
英文:

Captions of slider not visible (VPython)

问题

Here is the translated portion of your code:

  1. # Import libraries
  2. import vpython as vp
  3. # Create Canvas
  4. # Create objects
  5. # Define the planet
  6. # Define the atmosphere
  7. # Define the light source
  8. # Define the camera
  9. # Define the event handlers
  10. # Create the slider to rotate the light source around
  11. # Create the slider
  12. # Create slider to change light source wavelength
  13. # Define the white light button
  14. # Create the exit button
  15. # Run the simulation

If you need any specific part of the code translated, please let me know.

英文:

I have create a little 3d simulation with VPython. And with it sliders to control positions and colors or objects. However, I can't find a way to display a caption to indicate what is the slider about.

I have added an argument when defining the slider wtitle. I don't get any error message so I assume it is rightfuly defined but the caption is probably hidden or I am missing a step somewhere.

Here is my code:

  1. """ Import libraries """
  2. import vpython as vp
  3. """ Create Canvas """
  4. canvas = vp.canvas(width=1080, height=720)
  5. scene = vp.scene
  6. """ Create objects """
  7. # Define the planet
  8. planet_radius = 5
  9. # planet_texture = 'planet.jpg' # You can replace this with your own custom picture
  10. planet = vp.sphere(radius=planet_radius, texture=vp.textures.earth, shininess=0)
  11. def atm_opacity(wavelength):
  12. return atm_density * (550 / wavelength)**4
  13. # Define the atmosphere
  14. atm_thickness = 3.0 # You can modify this using a slide cursor
  15. atm_density = 0.5 # You can modify this using a slide cursor
  16. atm_radius = planet.radius+atm_thickness
  17. atm = vp.sphere(radius=atm_radius, opacity=0.25)
  18. atm.opacity_function = atm_opacity
  19. # Define the light source
  20. def light_color(wavelength):
  21. r, g, b = 0, 0, 0
  22. if wavelength >= 400 and wavelength < 440:
  23. r = -(wavelength - 440) / (440 - 400)
  24. b = 1.0
  25. elif wavelength >= 440 and wavelength < 490:
  26. g = (wavelength - 440) / (490 - 440)
  27. b = 1.0
  28. elif wavelength >= 490 and wavelength < 510:
  29. g = 1.0
  30. b = -(wavelength - 510) / (510 - 490)
  31. elif wavelength >= 510 and wavelength < 580:
  32. r = (wavelength - 510) / (580 - 510)
  33. g = 1.0
  34. elif wavelength >= 580 and wavelength < 645:
  35. r = 1.0
  36. g = -(wavelength - 645) / (645 - 580)
  37. elif wavelength >= 645 and wavelength <= 700:
  38. r = 1.0
  39. return vp.vector(r, g, b)
  40. light_type = 'red' # You can modify this using a drop-down menu
  41. # light_wavelength = 550 # You can modify this using a slide cursor
  42. light_size = 2 # You can modify this using a slide cursor
  43. light_intensity = 1.0 # You can modify this using a slide cursor
  44. pos_drift = 50
  45. light_xpos = atm_radius+pos_drift
  46. light_ypos = atm_radius+pos_drift
  47. light_zpos = atm_radius+pos_drift
  48. light_pos = vp.vector(light_xpos, light_ypos, light_zpos)
  49. wavelength = 400
  50. light = vp.local_light(pos=light_pos, color= light_color(wavelength), radius=light_size)
  51. light.color_function = light_color
  52. # Define the camera
  53. scene.autoscale = False
  54. scene.range = 10
  55. scene.forward = vp.vector(0, 0, -1)
  56. scene.up = vp.vector(0, 1, 0)
  57. scene.caption = 'Click and drag on the light source to move it. Use the drop-down menu and slide cursors to adjust its properties.'
  58. """ Define the event handlers """
  59. def on_light_down(evt):
  60. global light_dragging, light_drag_pos
  61. light_dragging = True
  62. light_drag_pos = evt.pos
  63. def on_light_move(evt):
  64. global light_dragging, light_drag_pos
  65. if light_dragging:
  66. light.pos += evt.pos - light_drag_pos
  67. light_drag_pos = evt.pos
  68. def on_light_up(evt):
  69. global light_dragging
  70. light_dragging = False
  71. # Bind the event handlers
  72. vp.scene.bind('mousedown', on_light_down)
  73. vp.scene.bind('mousemove', on_light_move)
  74. vp.scene.bind('mouseup', on_light_up)
  75. def on_mouse_down(event):
  76. global dragging, last_mouse_pos
  77. obj = scene.mouse.pick
  78. if obj == light:
  79. dragging = True
  80. last_mouse_pos = scene.mouse.pos
  81. canvas.bind('mousedown', on_mouse_down)
  82. # Create the slider to rotate the light source around
  83. def set_rotation_angle(slider):
  84. # light.rotate(angle=slider.value, axis=vp.vector(0, 1, 0), origin=planet.pos)
  85. # Calculate the rotation angle
  86. angle = vp.radians(slider.value)
  87. # Calculate the new position of the object
  88. x = atm_radius+pos_drift * vp.cos(angle)
  89. y = 0
  90. z = atm_radius+pos_drift * vp.sin(angle)
  91. # Set the position of the object
  92. light.pos = vp.vector(x, y, z)
  93. # Create the slider
  94. slider = vp.slider(wtitle='Rotate light source', min=0, max=360, step=1, value=0, bind=set_rotation_angle)
  95. # Create slider to change light source wavelength
  96. def WL_cursor(slider):
  97. val = slider.value
  98. new_wavelength = light_color(val)
  99. global light
  100. light.color = new_wavelength
  101. slider = vp.slider(wtitle='Change light source color', min=400, max=700, length=250, bind=WL_cursor)
  102. # Define the white light button
  103. def white():
  104. global light
  105. light.color = vp.color.white
  106. # Create the exit button
  107. white_button = vp.button(bind=white, text="White light")
  108. """ Run the simulation """
  109. while True:
  110. vp.rate(30)
  111. # Update the atmosphere
  112. atm.radius = planet.radius+atm_thickness
  113. atm.opacity = 0.25*atm_density
  114. # Update the light source
  115. light.radius = light_size
  116. light.intensity = light_intensity
  117. light.pos = light.pos
  118. # Update the camera
  119. vp.scene.center = planet.pos

答案1

得分: 2

你需要将滑块添加到canvas而不是scene,原因不明。所以可以这样操作:

  1. slider_angle = vp.slider(wtitle='旋转光源角度', min=0, max=360, step=1, value=0, bind=set_rotation_angle, right=15)
  2. canvas.append_to_caption('滑动光标以移动光源。')
  3. canvas.append_to_caption('\n\n')
英文:

Instead of adding the sliders to the scene you need to add it to the canvas for some reason. So something like:

  1. slider_angle = vp.slider(wtitle='Rotate light source', min=0, max=360, step=1, value=0, bind=set_rotation_angle, right=15)
  2. canvas.append_to_caption('Slide the cursor to move the light source.')
  3. canvas.append_to_caption('\n\n')

答案2

得分: 1

VPython会自动创建一个名为"scene"的画布(或在您的情况下是"vp.scene")。然后,您会说scene = vp.scene,并设置此画布的各种属性,比如scene.autoscale = False。但您还创建了第二个名为canvas的画布:canvas = vp.canvas(...)。大部分程序引用名为scene的画布,但您将"mousedown"绑定到了canvas,这会令人困惑。最清晰的做法是删除对"canvas"的创建,并将mousedown绑定到scene。

英文:

VPython automatically creates a canvas named "scene" (or in your case "vp.scene"). You then say scene = vp.scene and you set various attributes of this canvas, such as scene.autoscale = False. But you also create a second canvas, named canvas: canvas = vp.canvas(...). The most of your program references the canvas named scene, but you bind "mousedown" to canvas, which is confusing. The cleanest way to proceed is to delete the creation of "canvas" and bind mousedown to scene.

huangapple
  • 本文由 发表于 2023年4月13日 23:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定