【问题标题】:Callable object not callable可调用对象不可调用
【发布时间】:2021-08-21 02:59:33
【问题描述】:

我创建了一个函数,该函数使用用户的纬度和经度生成地图,并在下一次国际空间站将在用户位置上方时返回。我创建了一个按钮,使用 on_click 函数应该使用 lat 和 lon 作为变量生成地图,但显然,地图对象是不可调用的。我使用 generate_map 函数的callable 函数对此进行了测试,它返回 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

【问题讨论】:

  • 我不熟悉这个库,但你的意思可能是button.on_click(generate_map)。您当前正在调用该函数,然后将返回值传递给on_click,而不是传递该函数。同样在将来,请在跟踪中包含完整的错误。
  • 感谢您提供的信息,我不知道我正在这样做。当我按照您的建议运行它时,它返回一个类型错误:“generate_map() 采用 0 个位置参数,但给出了 1 个”。我对python比较陌生,你能澄清一下这是什么意思吗?
  • 这意味着on_click 期望能够为您的函数提供一些数据(如事件对象),但您的函数不需要任何参数。您的函数需要有一个参数来接受传递的参数。
  • 如果您是 Python 新手,您可能想退后一步,先做一个更简单的项目。没有良好的语言知识基础的 UI 编程可能会很痛苦。
  • 好的,非常感谢您的帮助。我会努力解决这个问题,看看我是否可以启动并运行它。干杯

标签: python callable ipyleaflet


【解决方案1】:

删除调用 button.on_click 的 generate_map 之后的括号;即:

button.on_click(generate_map)

您现在拥有的代码调用generate_map,因此您实际上是将其返回值(映射)传递给button.on_click。如果你不调用它(省略括号),你会将generate_map 函数本身传递给button.on_click,它是可调用的(并且是你在这里尝试做的)。

【讨论】:

  • 太棒了!非常感谢!这当然可以解决最初的错误,但我现在遇到了另一种类型的错误,“generate_map() 采用 0 个位置参数,但给出了 1 个”。我不确定这意味着什么,您有什么建议吗?
猜你喜欢
  • 2018-09-30
  • 2021-02-18
  • 1970-01-01
  • 2016-10-01
  • 2015-01-29
  • 2014-03-17
  • 2017-06-14
  • 2019-08-21
  • 2021-03-10
相关资源
最近更新 更多