[Python] How do I successfully assign a function to the on_click event?

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am new in this stuff, I am currently working in a school project about a matrix calculator, I want to enter the two matrix and that when I click a button the solution of the operation gets printed.

    import numpy as np
    from IPython.display import clear_output, display
    import ast
    import ipywidgets as widgets
    from ipywidgets.widgets import Output

    escalar = None
    output = Output()

    def in_matriz():
    print("ingrese la ",i+1," matriz en forma de lista de listas (Ejemplo: [1,2,3],[4,5,6],[7,8,9]))")
    return np.array(ast.literal_eval(input()))
    def in_escalar():
    print("ingrese el escalar/potencia")
    return int(input())

    def on_btn_clic(btn):
    with output:
    print(resultado_m)

    while True:

    print("Menú"+"\n1) Calculadora científica."+"\n2) Calculadora de matrices."+"\n3) Salir.")
    opcion = input("Seleccione una opción: ")
    match opcion:

    case "1":
    .
    .
    .

    case "2":
    while True:
    print("Has elegido Calculadora de matrices: ")

    operacion = input("#here there is the operation menu")
    continue

    mapping={
    #here I define all the operations
    }
    if operacion in ["1","2","3","4","6","8","9"]:
    rango = 2
    else:
    rango = 1

    for i in range(rango):
    if i==0:
    a_matriz=in_matriz()
    #here the operations that only need one matriz

    elif i==1:
    b_matriz=in_matriz()
    #here the operations that need two matriz

    if operacion in ["5","7"]:
    escalar=in_escalar()

    try:
    if operacion in ["10","11","12","13"]:
    resultado_m = mapping[operacion](a_matriz)
    def on_btn_clic(btn):
    with output:
    print(resultado_m)

    boton2 = widgets.Button(description="Ver resultado")
    boton2.on_click(on_btn_clic)
    display(boton2, output)

    elif operacion in ["5","7"]:
    resultado_m = mapping[operacion](escalar, a_matriz)
    def on_btn_clic(btn):
    with output:
    print(resultado_m)

    boton2 = widgets.Button(description="Ver resultado")
    boton2.on_click(on_btn_clic)
    display(boton2, output)
    else:
    resultado_m = mapping[operacion](a_matriz, b_matriz)
    def on_btn_clic(btn):
    with output:
    print(resultado_m)

    boton2 = widgets.Button(description="Ver resultado")
    boton2.on_click(on_btn_clic)
    display(boton2, output)

    except ValueError:
    .
    .
    .
    break


    I don´t think the issue is with the solution of the operation itself because I can see in the variable manager that it (resultado_m) has correctly assigned the solution of the operation. I think the problem is where I am putting everything, but idk. how it is right now it does not print anything, I have tried using display instead of print, and moving the function to inside the try block bc maybe the issue is that the variable is not being recognized outside the try block, however nothing helped and when I click the button nothing gets printed.

    Thanks in advance, if you need more details feel free to ask, I really need help with this

    Continue reading...

Compartilhe esta Página