-
Notifications
You must be signed in to change notification settings - Fork 26
/
example.py
executable file
·147 lines (98 loc) · 3.84 KB
/
example.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
'''
Example of using GooMPy with Tkinter
Copyright (C) 2015 Alec Singer and Simon D. Levy
This code is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this code. If not, see <http://www.gnu.org/licenses/>.
'''
import sys
if sys.version_info[0] == 2:
import Tkinter as tk
else:
import tkinter as tk
from PIL import ImageTk
from goompy import GooMPy
WIDTH = 800
HEIGHT = 500
LATITUDE = 37.7913838
LONGITUDE = -79.44398934
ZOOM = 15
MAPTYPE = 'roadmap'
class UI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry('%dx%d+500+500' % (WIDTH,HEIGHT))
self.title('GooMPy')
self.canvas = tk.Canvas(self, width=WIDTH, height=HEIGHT)
self.canvas.pack()
self.bind("<Key>", self.check_quit)
self.bind('<B1-Motion>', self.drag)
self.bind('<Button-1>', self.click)
self.label = tk.Label(self.canvas)
self.radiogroup = tk.Frame(self.canvas)
self.radiovar = tk.IntVar()
self.maptypes = ['roadmap', 'terrain', 'satellite', 'hybrid']
self.add_radio_button('Road Map', 0)
self.add_radio_button('Terrain', 1)
self.add_radio_button('Satellite', 2)
self.add_radio_button('Hybrid', 3)
self.zoom_in_button = self.add_zoom_button('+', +1)
self.zoom_out_button = self.add_zoom_button('-', -1)
self.zoomlevel = ZOOM
maptype_index = 0
self.radiovar.set(maptype_index)
self.goompy = GooMPy(WIDTH, HEIGHT, LATITUDE, LONGITUDE, ZOOM, MAPTYPE)
self.restart()
def add_zoom_button(self, text, sign):
button = tk.Button(self.canvas, text=text, width=1, command=lambda:self.zoom(sign))
return button
def reload(self):
self.coords = None
self.redraw()
self['cursor'] = ''
def restart(self):
# A little trick to get a watch cursor along with loading
self['cursor'] = 'watch'
self.after(1, self.reload)
def add_radio_button(self, text, index):
maptype = self.maptypes[index]
tk.Radiobutton(self.radiogroup, text=maptype, variable=self.radiovar, value=index,
command=lambda:self.usemap(maptype)).grid(row=0, column=index)
def click(self, event):
self.coords = event.x, event.y
def drag(self, event):
self.goompy.move(self.coords[0]-event.x, self.coords[1]-event.y)
self.image = self.goompy.getImage()
self.redraw()
self.coords = event.x, event.y
def redraw(self):
self.image = self.goompy.getImage()
self.image_tk = ImageTk.PhotoImage(self.image)
self.label['image'] = self.image_tk
self.label.place(x=0, y=0, width=WIDTH, height=HEIGHT)
self.radiogroup.place(x=0,y=0)
x = int(self.canvas['width']) - 50
y = int(self.canvas['height']) - 80
self.zoom_in_button.place(x= x, y=y)
self.zoom_out_button.place(x= x, y=y+30)
def usemap(self, maptype):
self.goompy.useMaptype(maptype)
self.restart()
def zoom(self, sign):
newlevel = self.zoomlevel + sign
if newlevel > 0 and newlevel < 22:
self.zoomlevel = newlevel
self.goompy.useZoom(newlevel)
self.restart()
def check_quit(self, event):
if ord(event.char) == 27: # ESC
exit(0)
UI().mainloop()