ipywidgets button only works once: use the same button multiple times

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

ipywidgets button only works once: use the same button multiple times

问题

I can help you translate the code comments and variable names. Here's the translated code:

  1. 我有以下类4个按钮可用
  2. - 下一张图片
  3. - 上一张图片
  4. - 最后一张图片
  5. - 第一张图片
  6. **jupyter notebook** 我想点击所需的按钮以跳转到关联的图片但是按钮**只能点击一次**相同的或另一个按钮),我无法浏览所有图片
  7. 如何使这个类中的按钮可以多次使用
  8. ```python
  9. class DisplayButtons:
  10. def __init__(self, low_path, restored_path):
  11. self.left = None
  12. self.right = None
  13. self.top_right = None
  14. self.top_left = None
  15. self.index = None
  16. self.box = None
  17. self.low_path = low_path
  18. self.restored_path = restored_path
  19. def plot_restoration(self):
  20. clear_output()
  21. display(self.box)
  22. img = imread(self.low_path[self.index])
  23. if img.ndim == 3: img = img[0]
  24. height, width = img.shape
  25. if height < 450: height, width = height * 2, width * 2
  26. img_restored = imread(self.restored_path[self.index])
  27. plt.figure(figsize = (height/50, width/50))
  28. plt.subplot(1, 2, 1)
  29. plt.imshow(img)
  30. plt.subplot(1, 2, 2)
  31. plt.imshow(img_restored)
  32. plt.show()
  33. def is_end(self):
  34. return self.index == len(self.low_path) - 1
  35. def is_start(self):
  36. return self.index == 0
  37. def is_displayed(self):
  38. return self.index is not None
  39. def go_top_left(self):
  40. if not self.is_start():
  41. self.initialize()
  42. def initialize(self):
  43. self.index=0
  44. self.plot_restoration()
  45. def go_left(self):
  46. if not self.is_displayed or self.is_start():
  47. self.initialize()
  48. else:
  49. self.index -= 1
  50. self.plot_restoration()
  51. def go_right(self):
  52. if not self.is_end():
  53. self.index += 1
  54. self.plot_restoration()
  55. def go_top_right(self):
  56. if not self.is_end():
  57. self.index = len(self.low_path) - 1
  58. self.plot_restoration()
  59. def set_buttons(self):
  60. # 定义按钮
  61. scroll = ['⇤', '←', '→', '⇥']
  62. layout = widgets.Layout(width='auto', height='auto')
  63. buttons = [Button(description=s, layout=layout) for s in scroll]
  64. button_1, button_2, button_3, button_4 = buttons
  65. box_1 = VBox([buttons[0]])
  66. box_2 = VBox([buttons[1]])
  67. box_3 = VBox([buttons[2]])
  68. box_4 = VBox([buttons[3]])
  69. self.box = HBox([box_1, box_2, box_3, box_4])
  70. # 单击时启动函数
  71. button_1.on_click(self.go_top_left())
  72. button_2.on_click(self.go_left())
  73. button_3.on_click(self.go_right())
  74. button_4.on_click(self.go_top_right())
  75. display(self.box)

Please note that the comments and variable names are translated, but the code itself remains in its original form.

英文:

I have the following class and I have 4 buttons to go to:

  • the next image
  • the previous image
  • the last image
  • the first image

Working with jupyter notebook, I want to click on the required button, which goes to the associated image. But the button only works once (the same or another button) and I can't go through all images.

How can I do to use buttons multiple times with this class?

  1. class DisplayButtons:
  2. def __init__(self, low_path, restored_path):
  3. self.left = None
  4. self.right = None
  5. self.top_right = None
  6. self.top_left = None
  7. self.index = None
  8. self.box = None
  9. self.low_path = low_path
  10. self.restored_path = restored_path
  11. def plot_restoration(self):
  12. clear_output()
  13. display(self.box)
  14. img = imread(self.low_path[self.index])
  15. if img.ndim == 3: img = img[0]
  16. height, width = img.shape
  17. if height &lt; 450: height, width = height * 2, width * 2
  18. img_restored = imread(self.restored_path[self.index])
  19. plt.figure(figsize = (height/50, width/50))
  20. plt.subplot(1, 2, 1)
  21. plt.imshow(img)
  22. plt.subplot(1, 2, 2)
  23. plt.imshow(img_restored)
  24. plt.show()
  25. def is_end(self):
  26. return self.index == len(self.low_path) - 1
  27. def is_start(self):
  28. return self.index == 0
  29. def is_displayed(self):
  30. return self.index is not None
  31. def go_top_left(self):
  32. if not self.is_start():
  33. self.initialize()
  34. def initialize(self):
  35. self.index=0
  36. self.plot_restoration()
  37. def go_left(self):
  38. if not self.is_displayed or self.is_start():
  39. self.initialize()
  40. else:
  41. self.index -= 1
  42. self.plot_restoration()
  43. def go_right(self):
  44. if not self.is_end():
  45. self.index += 1
  46. self.plot_restoration()
  47. def go_top_right(self):
  48. if not self.is_end():
  49. self.index = len(self.low_path) - 1
  50. self.plot_restoration()
  51. def set_buttons(self):
  52. # Define buttons
  53. scroll = [&#39;⇤&#39;, &#39;←&#39;, &#39;→&#39;, &#39;⇥&#39;]
  54. layout = widgets.Layout(width=&#39;auto&#39;, height=&#39;auto&#39;)
  55. buttons = [Button(description=s, layout = layout) for s in scroll]
  56. button_1, button_2, button_3, button_4 = buttons
  57. box_1 = VBox([buttons[0]])
  58. box_2 = VBox([buttons[1]])
  59. box_3 = VBox([buttons[2]])
  60. box_4 = VBox([buttons[3]])
  61. self.box = HBox([box_1, box_2, box_3, box_4])
  62. # Launch of function when click
  63. button_1.on_click(self.go_top_left())
  64. button_2.on_click(self.go_left())
  65. button_3.on_click(self.go_right())
  66. button_4.on_click(self.go_top_right())
  67. display(self.box)

Thanks for your help

答案1

得分: 1

  1. 传递方法引用而不是调用方法
  2. 为按钮的事件处理程序添加第二个参数,表示点击的按钮小部件实例。
  3. 您将需要一些其他逻辑更新,但前两点应足够涵盖如何多次使用按钮。
英文:
  1. Pass the method reference instead of calling the method

  2. Add a 2nd argument to the buttons' event handlers, representing the clicked button widget instance.

  3. You are going to need to some other logic updates, but the first two points should cover enough how to use the buttons multiple times

    1. from ipywidgets import Button, HBox, Layout, VBox
    2. class DisplayButtons:
    3. def __init__(self, low_path, restored_path):
    4. self.left = None
    5. self.right = None
    6. self.top_right = None
    7. self.top_left = None
    8. self.index = None
    9. self.box = None
    10. self.low_path = low_path
    11. self.restored_path = restored_path
    12. def plot_restoration(self):
    13. clear_output()
    14. display(self.box)
    15. img = imread(self.low_path[self.index])
    16. if img.ndim == 3: img = img[0]
    17. height, width = img.shape
    18. if height &lt; 450: height, width = height * 2, width * 2
    19. img_restored = imread(self.restored_path[self.index])
    20. plt.figure(figsize = (height/50, width/50))
    21. plt.subplot(1, 2, 1)
    22. plt.imshow(img)
    23. plt.subplot(1, 2, 2)
    24. plt.imshow(img_restored)
    25. plt.show()
    26. def is_end(self):
    27. return self.index == len(self.low_path) - 1
    28. def is_start(self):
    29. return self.index == 0
    30. def is_displayed(self):
    31. return self.index is not None
    32. def go_top_left(self, b): # &lt;- 2. button argument
    33. if not self.is_start():
    34. self.initialize()
    35. def initialize(self):
    36. self.index=0
    37. self.plot_restoration()
    38. def go_left(self, b): # &lt;- 2. button argument
    39. if not self.is_displayed or self.is_start():
    40. self.initialize()
    41. else:
    42. self.index -= 1
    43. self.plot_restoration()
    44. def go_right(self, b): # &lt;- 2. button argument
    45. if not self.is_end():
    46. self.index += 1
    47. self.plot_restoration()
    48. def go_top_right(self, b): # &lt;- 2. button argument
    49. if not self.is_end():
    50. self.index = len(self.low_path) - 1
    51. self.plot_restoration()
    52. def set_buttons(self):
    53. # Define buttons
    54. scroll = [&#39;⇤&#39;, &#39;←&#39;, &#39;→&#39;, &#39;⇥&#39;]
    55. layout = widgets.Layout(width=&#39;auto&#39;, height=&#39;auto&#39;)
    56. buttons = [Button(description=s, layout = layout) for s in scroll]
    57. button_1, button_2, button_3, button_4 = buttons
    58. box_1 = VBox([buttons[0]])
    59. box_2 = VBox([buttons[1]])
    60. box_3 = VBox([buttons[2]])
    61. box_4 = VBox([buttons[3]])
    62. self.box = HBox([box_1, box_2, box_3, box_4])
    63. # Launch of function when click
    64. button_1.on_click(self.go_top_left) # &lt;- 1. method reference
    65. button_2.on_click(self.go_left) # &lt;- 1. method reference
    66. button_3.on_click(self.go_right) # &lt;- 1. method reference
    67. button_4.on_click(self.go_top_right) # &lt;- 1. method reference
    68. display(self.box)

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

发表评论

匿名网友

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

确定