First time here? Check out the FAQ!

Ask Your Question
1

Alternative to using ButtonBar in SageMath (interact)

asked 0 years ago

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!

Preview: (hide)

Comments

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

eric_g gravatar imageeric_g ( 0 years ago )

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

InnesInes gravatar imageInnesInes ( 0 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 0 years ago

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)"))
Preview: (hide)
link

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: 0 years ago

Seen: 131 times

Last updated: Aug 12