forked from TomSchimansky/TkinterMapView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas_button.py
More file actions
76 lines (61 loc) · 3.46 KB
/
canvas_button.py
File metadata and controls
76 lines (61 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import tkinter
import sys
import math
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .map_widget import TkinterMapView
class CanvasButton:
def __init__(self, map_widget: "TkinterMapView", canvas_position, text="", command=None):
self.map_widget = map_widget
self.canvas_position = canvas_position
if sys.platform == "darwin":
self.width = 16
self.height = 16
self.border_width = 16
else:
self.width = 29
self.height = 29
self.border_width = 3
self.text = text
self.command = command
self.canvas_rect = None
self.canvas_text = None
self.draw()
def click(self, event):
if self.command is not None:
self.command()
def hover_on(self, event):
if self.canvas_rect is not None:
self.map_widget.canvas.itemconfig(self.canvas_rect, fill="gray50", outline="gray50")
if sys.platform == "darwin":
self.map_widget.canvas.config(cursor="pointinghand")
elif sys.platform.startswith("win"):
self.map_widget.canvas.config(cursor="hand2")
else:
self.map_widget.canvas.config(cursor="hand2") # not tested what it looks like on Linux!
def hover_off(self, event):
if self.canvas_rect is not None:
self.map_widget.canvas.itemconfig(self.canvas_rect, fill="gray20", outline="gray20")
self.map_widget.canvas.config(cursor="arrow")
def draw(self):
self.canvas_rect = self.map_widget.canvas.create_polygon(self.canvas_position[0], self.canvas_position[1],
self.canvas_position[0] + self.width, self.canvas_position[1],
self.canvas_position[0] + self.width,
self.canvas_position[1] + self.height,
self.canvas_position[0], self.canvas_position[1] + self.height,
width=self.border_width,
fill="gray20", outline="gray20",
tag="button")
self.canvas_text = self.map_widget.canvas.create_text(math.floor(self.canvas_position[0] + self.width / 2),
math.floor(self.canvas_position[1] + self.height / 2),
anchor=tkinter.CENTER,
text=self.text,
fill="white",
font="Tahoma 16",
tag="button")
self.map_widget.canvas.tag_bind(self.canvas_rect, "<Button-1>", self.click)
self.map_widget.canvas.tag_bind(self.canvas_text, "<Button-1>", self.click)
self.map_widget.canvas.tag_bind(self.canvas_rect, "<Enter>", self.hover_on)
self.map_widget.canvas.tag_bind(self.canvas_text, "<Enter>", self.hover_on)
self.map_widget.canvas.tag_bind(self.canvas_rect, "<Leave>", self.hover_off)
self.map_widget.canvas.tag_bind(self.canvas_text, "<Leave>", self.hover_off)