我正在尝试使用tkinter制作一个地图程序,但是我无法创建单击时或鼠标经过时显示信息的图标。
root = Tk()
root.title('test')
root.geometry("500x500")
def text_map_icon(canvas, origin_x, origin_y, text):
canvas.create_text(origin_x, origin_y - 60, text=text)
def map_icon(canvas, origin_x, origin_y, text):
icon = canvas.create_arc((origin_x - 50), (origin_x - 50), (origin_y + 50), (origin_y + 50), start=70, extent=40,
fill='green', activefill='red', activewidth=2.0)
canvas.tag_bind(icon, '<Enter>', text_map_icon(c, origin_x, origin_y, text))
map_icon(c, 250, 250, 'map icon')
c.pack()
root.mainloop()
为此,我使用create_arc canvas方法创建了一个地图图标。为了在单击图标时显示信息,我尝试将其绑定到text_map_icon函数。为了使该文本在地图上显示相关信息(可根据输入的数据而变化),我需要该函数具有用于文本和坐标的参数。
我了解到,通常在绑定函数时,函数的参数为“事件”,而在调用时不给出任何参数。有没有办法将函数与参数绑定?
答案 0 :(得分:0)
您可以使用lambda
:
from tkinter import *
root = Tk()
root.title('test')
root.geometry("500x500")
c = Canvas(root)
def text_map_icon(event, canvas, origin_x, origin_y, text):
canvas.create_text(origin_x, origin_y - 60, text=text)
def map_icon(canvas, origin_x, origin_y, text):
icon = canvas.create_arc((origin_x - 50), (origin_x - 50), (origin_y + 50), (origin_y + 50), start=70, extent=40,
fill='green', activefill='red', activewidth=2.0)
canvas.tag_bind(icon, '<Enter>', lambda e: text_map_icon(e, c, origin_x, origin_y, text))
map_icon(c, 250, 250, 'map icon')
c.pack()
root.mainloop()