我创建了以下代码,但是当我单击click me
按钮时,出现以下错误消息:
TypeError: 'mpfr' object is not callable
有人会知道代码有什么问题吗?
import gmpy2 as g
from ipywidgets import widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)
max_precision = g.get_max_precision()
pi = g.const_pi()
g.set_context(g.context())
def set_bits_precision(decimal_precision):
bits_precision = int(decimal_precision/g.log(2))
if (bits_precision > max_precision): bits_precision = max_precision
ctx = g.get_context()
ctx.precision = bits_precision
return
def square_root(number):
return g.sqrt(number)
def circle_perimeter(radius):
return 2*pi*radius
def on_button_clicked(x):
return square_root(x)
set_bits_precision(10)
print(pi)
button.on_click(on_button_clicked(2))
答案 0 :(得分:0)
button.on_click
提供回调函数。您传递的是on_button_clicked
的结果,而不是传入函数,该参数使用参数2(因此,字面值2的平方根)求值。您可以使用部分函数评估来完成所需的工作,方法是将最后一行代码替换为:
import functools
button.on_click(functools.partial(on_button_clicked, 2))