1

For example, I have a Slider with a callback set via on_changed(). I have the option of modifying the Slider value with set_val() without triggering the callback by setting eventson to False before setting the value.

However, if I set eventson to False for the TextBox, then update the value with set_val(), the callback on_submit() still gets triggered.

The source code for my version of Matplotlib verifies this:

For TextBox:

def set_val(self, val):
    newval = str(val)
    if self.text == newval:
        return
    self.text = newval
    self.text_disp.remove()
    self.text_disp = self._make_text_disp(self.text)
    self._rendercursor()
    self._notify_change_observers()
    self._notify_submit_observers()

For Slider:

def set_val(self, val):
    """
    Set slider value to *val*

    Parameters
    ----------
    val : float
    """
    xy = self.poly.xy
    xy[2] = val, 1
    xy[3] = val, 0
    self.poly.xy = xy
    self.valtext.set_text(self.valfmt % val)
    if self.drawon:
        self.ax.figure.canvas.draw_idle()
    self.val = val
    if not self.eventson:
        return
    for cid, func in self.observers.items():
        func(val)

Is there a preferred way to do this?

1 Answer 1

1

It's a dirty solution, but you could "monkey patch" the class TextBox to call your own set_val() instead. Unfortunately, I don't know whether this could have unintended consequences.

from matplotlib.widgets import TextBox

def set_val(self, val):
    newval = str(val)
    if self.text == newval:
        return
    self.text = newval
    self.text_disp.remove()
    self.text_disp = self._make_text_disp(self.text)
    self._rendercursor()
    if self.eventson:
        self._notify_change_observers()
        self._notify_submit_observers()

TextBox.set_val = set_val

(...)

tbox1 = TextBox(tax1, 'Text Box 1')
tbox1.on_submit(...)

tbox1.eventson = False
tbox1.set_val('test')
tbox1.eventson = True

plt.show()

Alternatively, for a better, long term solution, you could raise an issue, or do a pull request at matplotlib's tracker Nevermind, I see you already raised an issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok. I'll ask the devs to do a pull request and patch the issue... I don't know if doing this for a textbox was intentional or not though, so maybe there's a reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.