<tfoot id='nM8hv'></tfoot>
        <bdo id='nM8hv'></bdo><ul id='nM8hv'></ul>

    1. <legend id='nM8hv'><style id='nM8hv'><dir id='nM8hv'><q id='nM8hv'></q></dir></style></legend>

      1. <small id='nM8hv'></small><noframes id='nM8hv'>

      2. <i id='nM8hv'><tr id='nM8hv'><dt id='nM8hv'><q id='nM8hv'><span id='nM8hv'><b id='nM8hv'><form id='nM8hv'><ins id='nM8hv'></ins><ul id='nM8hv'></ul><sub id='nM8hv'></sub></form><legend id='nM8hv'></legend><bdo id='nM8hv'><pre id='nM8hv'><center id='nM8hv'></center></pre></bdo></b><th id='nM8hv'></th></span></q></dt></tr></i><div id='nM8hv'><tfoot id='nM8hv'></tfoot><dl id='nM8hv'><fieldset id='nM8hv'></fieldset></dl></div>
      3. 链接 ipython 小部件按钮和滑块值

        时间:2023-09-03
        <i id='WrZ9M'><tr id='WrZ9M'><dt id='WrZ9M'><q id='WrZ9M'><span id='WrZ9M'><b id='WrZ9M'><form id='WrZ9M'><ins id='WrZ9M'></ins><ul id='WrZ9M'></ul><sub id='WrZ9M'></sub></form><legend id='WrZ9M'></legend><bdo id='WrZ9M'><pre id='WrZ9M'><center id='WrZ9M'></center></pre></bdo></b><th id='WrZ9M'></th></span></q></dt></tr></i><div id='WrZ9M'><tfoot id='WrZ9M'></tfoot><dl id='WrZ9M'><fieldset id='WrZ9M'></fieldset></dl></div>
        <legend id='WrZ9M'><style id='WrZ9M'><dir id='WrZ9M'><q id='WrZ9M'></q></dir></style></legend>

        <small id='WrZ9M'></small><noframes id='WrZ9M'>

          <tbody id='WrZ9M'></tbody>
      4. <tfoot id='WrZ9M'></tfoot>

            <bdo id='WrZ9M'></bdo><ul id='WrZ9M'></ul>

                  本文介绍了链接 ipython 小部件按钮和滑块值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图弄清楚如何将按钮小部件控制的计数器的值链接到滑块小部件的值.

                  I'm trying to figure out how to link the value of a counter controlled by button widgets to the value of a slider widget.

                  这里的目标是使用 ipython 小部件创建一个简单的类似 vcr"的界面,其中包含三个小部件:一个 IntSlider 和两个递增计数器和递减的 Button一个柜台.这就是我所拥有的:

                  The goal here is use ipython widgets to create a simple "vcr-like" interface with three widgets: an IntSlider and two Buttons that increment a counter and decrement a counter. This is what I've got:

                  import ipywidgets as widgets
                  from functools import partial
                  from IPython.display import display
                  import traitlets
                  
                  class Counter:
                     def __init__(self, initial=0):
                        self.value = initial
                  
                     def increment(self, amount=1):
                        self.value += amount
                        return self.value
                  
                  def button_plus(counter, w):
                      counter.increment(+1)  
                  
                  def button_minus(counter, w):
                      counter.increment(-1) 
                  
                  counter = Counter()
                  # 1 step forward button
                  wplus = widgets.Button(description='>')
                  wplus.on_click(partial(button_plus, counter))
                  # 1 step backward button
                  wminus = widgets.Button(description='<')
                  wminus.on_click(partial(button_minus, counter))
                  # integer slider
                  wpick = widgets.IntSlider(value=0,min=0,max=10,step=1,description="time step")
                  
                  display(wminus, wpick, wplus)
                  
                  print(counter.value)
                  print(wpick.value)
                  

                  这是一个屏幕截图,我将 IntSlider 移动到 1 并在增量按钮上单击了两次:

                  and here's a screen grab where I've moved the IntSlider to 1 and clicked twice on the increment button:

                  我显然希望有一个整数值由所有 3 个小部件控制并与之同步.

                  I'd obviously like there to be a single integer value being controlled by and be in sync with all 3 widgets.

                  我阅读了 widget 链接,但我看不到如何执行此操作,因为我的按钮小部件没有值 - 计数器对象具有我想要链接的值.

                  I read about widget linking but I don't see how to do this since my button widgets don't have a value -- the counter object has the value I want to link.

                  这不起作用:

                  l = traitlets.link((counter, 'value'), (wpick, 'value'))
                  

                  因为 counter 不是 HasTraits.

                  如何将 counter.value 链接到 wpick.value 以便单击其中一个按钮将调整滑块上的 int?

                  How can I get counter.value to be linked to wpick.value so that clicking on one of the buttons will adjust the int on the slider?

                  推荐答案

                  如下this 指南,您需要 Counter 类从 DOMWidget 类继承,如下所示:

                  Following this guide, you need the Counter class to inheret from the DOMWidget class like this:

                  from traitlets import CInt, link
                  class Counter(widgets.DOMWidget):
                      value = CInt(0, sync=True)
                  

                  然后您可以定义您的 counter 小部件和按钮回调方法:

                  You can then define your counter widget and button callback methods:

                  counter = Counter()
                  def button_plus(name):
                      counter.value += 1 if counter.value < 10 else 0
                  def button_minus(name):
                      counter.value -= 1 if counter.value > 0 else 0
                  

                  链接 slidercounter 小部件:

                  link the slider and counter widgets:

                  link((wpick, 'value'), (counter, 'value'))
                  

                  并在按钮上注册事件:

                  wplus.on_click(button_plus)
                  wminus.on_click(button_minus)
                  

                  现在单击按钮将增加/减少计数器的值.

                  Clicking the buttons will now in/decrease the value of the counter.

                  这篇关于链接 ipython 小部件按钮和滑块值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Tkinter 全局绑定 下一篇:有没有办法在 Python 中创建无边框透明窗口?

                  相关文章

                      <bdo id='J1T4L'></bdo><ul id='J1T4L'></ul>
                  1. <small id='J1T4L'></small><noframes id='J1T4L'>

                    <i id='J1T4L'><tr id='J1T4L'><dt id='J1T4L'><q id='J1T4L'><span id='J1T4L'><b id='J1T4L'><form id='J1T4L'><ins id='J1T4L'></ins><ul id='J1T4L'></ul><sub id='J1T4L'></sub></form><legend id='J1T4L'></legend><bdo id='J1T4L'><pre id='J1T4L'><center id='J1T4L'></center></pre></bdo></b><th id='J1T4L'></th></span></q></dt></tr></i><div id='J1T4L'><tfoot id='J1T4L'></tfoot><dl id='J1T4L'><fieldset id='J1T4L'></fieldset></dl></div>
                  2. <tfoot id='J1T4L'></tfoot>

                      <legend id='J1T4L'><style id='J1T4L'><dir id='J1T4L'><q id='J1T4L'></q></dir></style></legend>