Skip to content

Commit 3ce10d6

Browse files
committed
add optional marker menu
A custom right click menu can be added to a marker with myMenu = { # key is the title and value is the function to call 'My fancy marker menu entry 1': function1, # separators can also be added '-': None, 'And a second entry after the separator': function2 } map_widget.set_marker(x, y, menu=myMenu)
1 parent eb84f60 commit 3ce10d6

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

tkintermapview/canvas_position_marker.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def __init__(self,
2020
command: Callable = None,
2121
image=None,
2222
image_zoom_visibility: tuple = (13, float("inf")),
23-
data: any = None):
23+
data: any = None,
24+
menu: any = None):
2425

2526
self.map_widget = map_widget
2627
self.position = position
@@ -33,7 +34,9 @@ def __init__(self,
3334
self.image_zoom_visibility = image_zoom_visibility
3435
self.deleted = False
3536
self.command = command
37+
self.menu = menu
3638
self.data = data
39+
self.mouse_over = False
3740

3841
self.polygon = None
3942
self.big_circle = None
@@ -70,6 +73,7 @@ def hide_image(self, image_hidden):
7073
self.draw()
7174

7275
def mouse_enter(self, event=None):
76+
self.mouse_over = True
7377
if sys.platform == "darwin":
7478
self.map_widget.canvas.config(cursor="pointinghand")
7579
elif sys.platform.startswith("win"):
@@ -78,12 +82,23 @@ def mouse_enter(self, event=None):
7882
self.map_widget.canvas.config(cursor="hand2") # not tested what it looks like on Linux!
7983

8084
def mouse_leave(self, event=None):
85+
self.mouse_over = False
8186
self.map_widget.canvas.config(cursor="arrow")
8287

8388
def click(self, event=None):
8489
if self.command is not None:
8590
self.command(self)
8691

92+
def click_right(self, event=None):
93+
if self.menu is not None:
94+
m = tkinter.Menu(self.map_widget, tearoff=0)
95+
for title, cmd in self.menu.items():
96+
if title == '-':
97+
m.add_separator()
98+
continue
99+
m.add_command(label=f"{title}", command=cmd)
100+
m.tk_popup(event.x_root, event.y_root) # display menu
101+
87102
def get_canvas_pos(self, position):
88103
tile_position = decimal_to_osm(*position, round(self.map_widget.zoom))
89104

@@ -110,6 +125,10 @@ def draw(self, event=None):
110125
self.map_widget.canvas.tag_bind(self.polygon, "<Enter>", self.mouse_enter)
111126
self.map_widget.canvas.tag_bind(self.polygon, "<Leave>", self.mouse_leave)
112127
self.map_widget.canvas.tag_bind(self.polygon, "<Button-1>", self.click)
128+
if sys.platform == "darwin":
129+
self.map_widget.canvas.tag_bind(self.polygon, "<Button-2>", self.click_right)
130+
else:
131+
self.map_widget.canvas.tag_bind(self.polygon, "<Button-3>", self.click_right)
113132
else:
114133
self.map_widget.canvas.coords(self.polygon,
115134
canvas_pos_x - 14, canvas_pos_y - 23,
@@ -124,6 +143,10 @@ def draw(self, event=None):
124143
self.map_widget.canvas.tag_bind(self.big_circle, "<Enter>", self.mouse_enter)
125144
self.map_widget.canvas.tag_bind(self.big_circle, "<Leave>", self.mouse_leave)
126145
self.map_widget.canvas.tag_bind(self.big_circle, "<Button-1>", self.click)
146+
if sys.platform == "darwin":
147+
self.map_widget.canvas.tag_bind(self.big_circle, "<Button-2>", self.click_right)
148+
else:
149+
self.map_widget.canvas.tag_bind(self.big_circle, "<Button-3>", self.click_right)
127150
else:
128151
self.map_widget.canvas.coords(self.big_circle,
129152
canvas_pos_x - 14, canvas_pos_y - 45,
@@ -141,6 +164,10 @@ def draw(self, event=None):
141164
self.map_widget.canvas.tag_bind(self.canvas_text, "<Enter>", self.mouse_enter)
142165
self.map_widget.canvas.tag_bind(self.canvas_text, "<Leave>", self.mouse_leave)
143166
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-1>", self.click)
167+
if sys.platform == "darwin":
168+
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-2>", self.click_right)
169+
else:
170+
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-3>", self.click_right)
144171
else:
145172
self.map_widget.canvas.coords(self.canvas_text, canvas_pos_x, canvas_pos_y - 56)
146173
self.map_widget.canvas.itemconfig(self.canvas_text, text=self.text)

tkintermapview/map_widget.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ def convert_canvas_coords_to_decimal_coords(self, canvas_x: int, canvas_y: int)
202202
def mouse_right_click(self, event):
203203
coordinate_mouse_pos = self.convert_canvas_coords_to_decimal_coords(event.x, event.y)
204204

205+
# check if mouse is over a marker with menu
206+
for m in self.canvas_marker_list:
207+
if m.mouse_over and m.menu:
208+
# don't show right click menu of map
209+
return
210+
205211
def click_coordinates_event():
206212
try:
207213
pyperclip.copy(f"{coordinate_mouse_pos[0]:.7f} {coordinate_mouse_pos[1]:.7f}")

0 commit comments

Comments
 (0)