Ask Your Question
1

Alternative to using ButtonBar in SageMath (interact)

asked 2025-07-10 14:42:37 +0200

InnesInes gravatar image

Hello everyone,

I'm currently trying my hand at an interactive calculator, coding it in Sagemath. I quickly got a good result for the keypad using ButtonBar:

eingabe = " " 

@interact
def tasten(z = ButtonBar(
    ["(", ")", ".", "+", 
     "1", "2", "3", "-", 
     "4", "5", "6", "*", 
     "7", "8", "9", "/", 
     "=", "0", "⟳", "←" ],
    ncols=4, width="4em", label=" ")):

    global eingabe


    if z == "⟳":
        eingabe  = " "

    elif z == "←":
        eingabe  = eingabe[:-1]


    elif z == "=":
        ergebnis = sage_eval(eingabe)
        eingabe += z
        eingabe += str(ergebnis)


    else:
        eingabe += z

    print("Rechnung:", eingabe)

Unfortunately, ButtonBar only works in SageCell Server, but not in Jupyter Notebook with Sage 9.5.

I have researched the topic in various forums and usually selector (with the option buttons=True) is mentioned as an alternative to ButtonBar. However, since selector is about choosing something from various options, it is not intended that a same button is pressed twice. For my calculator this means that the buttons do not react several times in succession and therefore no numbers with the same digits can be entered (e.g. 11, 22, etc):

eingabe = " " 


@interact
def tasten(z = selector(
    ["(", ")", ".", "+", 
     "1", "2", "3", "-", 
     "4", "5", "6", "*", 
     "7", "8", "9", "/", 
     "=", "0", "⟳", "←" ],
    ncols=4, width="4em", label=" ",buttons=True, default = "0")):

    global eingabe


    if z == "⟳":
        eingabe  = " "

    elif z == "←":
        eingabe  = eingabe[:-1]



    elif z == "=":
        ergebnis = sage_eval(eingabe)
        eingabe += z
        eingabe += str(ergebnis)


    else:
        eingabe += z

    print("Rechnung:", eingabe)

Does anyone have any idea how I can get the same functionality as in the above example with ButtonBar, but with a code that also works in my Jupyter Notebook?

Thank you in advance!

edit retag flag offensive close merge delete

Comments

Sage 9.5 is pretty old by now. Can't you install Sage 10.6 on your computer?

eric_g gravatar imageeric_g ( 2025-07-16 11:29:57 +0200 )edit

I think that's basically a good idea. But it probably won't help me with the problem mentioned above either...

InnesInes gravatar imageInnesInes ( 2025-07-18 10:31:19 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2025-08-12 10:33:04 +0200

saraedum gravatar image

I don't think that there's an exact equivalent for the SageCell specific ButtonBar component.

Usually, you'd use ipywidgets to build something like this in a notebook. Here's a rough demo:

from ipywidgets import Button, GridBox, Layout, Text

value = Text()

buttons = []
for label in ["(", ")", ".", "+", "1", "2", "3", "-", "4", "5", "6", "*", "7", "8", "9", "/", "=", "0", "⟳", "←" ]:
    button = Button(description=label)
    def on_click(_, label=label):
        # Note the late binding workaround (label=label) without it all buttons are going to do the same)
        # TODO: Add your logic here.
        value.value += label
    button.on_click(on_click)
    buttons.append(button)

GridBox(buttons + [value], layout=Layout(grid_template_columns="repeat(4, 100px)"))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2025-07-10 14:42:37 +0200

Seen: 103 times

Last updated: Aug 12