我创建了一个函数,该函数生成包含用户纬度和经度的地图,并在下一次国际空间站经过用户位置时返回。我创建了一个按钮,使用 on_click 函数应该使用 lat 和 lon 作为变量生成地图,但显然,地图对象不可调用。我使用 callable
函数对 generate_map 函数进行了测试,它返回 True 表明该函数是可调用的。下面是代码。任何提示将不胜感激。
#Retrieve ISS location from the web
url = 'http://api.open-notify.org/iss-now.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
#Define the location as latitude and longtitude
location = result['iss_position']
lat = float(location['latitude'])
lon = float(location['longitude'])
#Generate a map illustrating the current position of the ISS
center = [lat, lon] # The centre of the map will be the latitude and longtitude of the ISS
zoom = 1.5
m = Map(basemap=basemaps.Esri.WorldImagery, center=center, zoom=zoom, close_popup_on_click=False)
# Mark the position of the ISS on the map
icon1 = AwesomeIcon(name='space-shuttle', marker_color='blue', icon_color='white', spin=False)
marker = Marker(icon=icon1, location=(lat, lon), draggable=False) # Add a marker at the current location of the ISS that cannot be moved
m.add_layer(marker)
#Recieve the users latitude
input_text_lat = widgets.IntText('-35')
input_text_lat
#Recieve the users longitude
input_text_lon = widgets.IntText('150')
input_text_lon
def generate_map():
user_location = input_text_lat.value, input_text_lon.value
url ='http://api.open-notify.org/iss-pass.json'
url = url + '?lat=' + str(input_text_lat.value) + '&lon=' + str(input_text_lon.value) #The URL requires inputs for lat and lon. The users location are the inputs here.
response = urllib.request.urlopen(url)
result = json.loads(response.read())
#Convert the Unix Timestamp to date and time format
over = (result['response'][1]['risetime'])
date_time = datetime.datetime.utcfromtimestamp(over).strftime('%d-%m-%Y at %H:%M:%S') #These place holders define the format of the displayed date and time.
print('The ISS will pass over you on the', date_time)
# Mark the position of the user on the map and display a message with the date and time of the next passover.
icon2 = AwesomeIcon(name='user', marker_color='lightred', icon_color='white', spin=False)
marker2 = Marker(icon=icon2, location=user_location, draggable=False, title='Location') #Add marker at the current location of the ISS that cannot be moved
m.add_layer(marker2) # Add layer that includes the users position
return m
button = widgets.Button(
description='Generate map',
disabled=False,
button_style='success', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Click me',
)
button.on_click(generate_map())
button
答案 0 :(得分:1)
去掉 generate_map 后面调用 button.on_click 的括号;即:
button.on_click(generate_map)
您现在拥有的代码调用 generate_map
,因此您实际上是将其返回值(地图)传递给 button.on_click
。如果您不调用它(省略括号),您会将 generate_map
函数本身传递给 button.on_click
,它是可调用的(这就是您在此处尝试执行的操作)。< /p>