Search code examples
pythonwidgetinteractiveipywidgets

Python add currency sign ipywidgets FloatText


I have the following widgets and I would like to see the dollar sign after the value in the float text box like 200 $ and also in the cW widget, however I cannot figure out how to add it.

from ipywidgets import DatePicker, Dropdown, FloatText, interact, interactive, Layout,  VBox, HBox, interactive_output, Text

aW = Dropdown(options = [100, 50, 25])
bW = FloatText(200)
cW = FloatText()

ui = HBox([aW, bW, cW])


def f(a, b, c):
    cW.value = a + b

out = interactive_output(f, {'a': aW, 'b': bW, 'c': cW})

display(ui, out)

Solution

  • The issue is you have chosen cW to be float text, so therefore you cannot display a string like '200$'. You would need to use cW = ipywidgets.Text(), and then set the string in f:

    def f(a,b,c):
        cW.value = f'{a + b} $'
    

    The downside of this is that you cannot get the value of cW as a numeric value directly, you would need to parse it out.