如果要更改on_success函数的值以使用另一个参数,请使用kivy https://kivy.org/doc/stable/api-kivy.network.urlrequest.html中的URL对象,如何将值传递给它?
def generate_images(sensor_id):
req = UrlRequest(URL, on_success=url_success)
然后在on_success中具有这样的内容
def url_success(req, result, sensor_id):
答案 0 :(得分:0)
一种解决方案是使用functools.partial()
:
from functools import partial
# ...
def generate_images(sensor_id):
req = UrlRequest(URL, on_success=partial(url_success, sensor_id))
# ...
def url_success(sensor_id, req, result):
print(sensor_id, req, result)
另一种解决方案是使用lambda
函数:
def generate_images(sensor_id):
req = UrlRequest(URL, on_success= lambda req, result, sensor_id=sensor_id : url_success(req, result, sensor_id))