我和 TheInterestedOne在此处提出的问题相同。
我需要为用户创建两个按钮,并建议用户点击循环中的两个按钮之一;以便循环的下一次迭代仅在用户选择之后发生。
我读过这个来源,但我不能让它适用于按钮。我不明白,在使用按钮的情况下,小部件属性如何变化。
from functools import wraps
def yield_for_change(widget, attribute):
def f(iterator):
@wraps(iterator)
def inner():
i = iterator()
def next_i(change):
try:
i.send(change.new)
except StopIteration as e:
widget.unobserve(next_i, attribute)
widget.observe(next_i, attribute) //**button.on_click(on_button_clicked)
may be?**
# start the generator
next(i)
return inner
return f
from ipywidgets import Button
button=Button()
def on_button_clicked():
print("Button clicked.")
@yield_for_change(button, 'value')
def f():
for i in range(10):
print('did work %s'%i)
x = yield
button.on_click(on_button_clicked)
Run Code Online (Sandbox Code Playgroud) 我有一个由中心坐标和半径给出的圆列表,以及由坐标给出的一些点。需要检查圆是否在给定点周围形成封闭区域。该点不位于任何圆圈内。例如,这里我们看到点周围的封闭区域
在这里 - 未封闭区域:
我过滤了圆圈(不是最好的方法 - 如何做得更好?),但我不知道,如何建立成对的交集。
def check_inside(c1, r1, c2, r2):
if r1 < r2:
c1, c2 = c2, c1
r1, r2 = r2, r1
if r1 > (((c1[0] - c2[0]) ** 2 + (c1[1] - c2[1]) ** 2) ** 0.5 + r2):
return c2, r2
else:
return -1
def check_intersection(c1, r1, c2, r2):
if (r1 + r2 >= ((c1[0] - c2[0]) ** 2 + (c1[1] - c2[1]) ** 2) ** 0.5) and check_inside(c1, r1, …Run Code Online (Sandbox Code Playgroud)