-
Notifications
You must be signed in to change notification settings - Fork 46
/
pimenu.py
executable file
·252 lines (208 loc) · 6.84 KB
/
pimenu.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import Tkconstants as TkC
import os
import subprocess
import sys
from Tkinter import Tk, Frame, Button, Label, PhotoImage
from math import sqrt, floor, ceil
import yaml
class FlatButton(Button):
def __init__(self, master=None, cnf=None, **kw):
Button.__init__(self, master, cnf, **kw)
self.config(
compound=TkC.TOP,
relief=TkC.FLAT,
bd=0,
bg="#b91d47", # dark-red
fg="white",
activebackground="#b91d47", # dark-red
activeforeground="white",
highlightthickness=0
)
def set_color(self, color):
self.configure(
bg=color,
fg="white",
activebackground=color,
activeforeground="white"
)
class PiMenu(Frame):
framestack = []
icons = {}
path = ''
lastinit = 0
def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.pack(fill=TkC.BOTH, expand=1)
self.path = os.path.dirname(os.path.realpath(sys.argv[0]))
self.initialize()
def initialize(self):
"""
(re)load the the items from the yaml configuration and (re)init
the whole menu system
:return: None
"""
with open(self.path + '/pimenu.yaml', 'r') as f:
doc = yaml.load(f)
self.lastinit = os.path.getmtime(self.path + '/pimenu.yaml')
if len(self.framestack):
self.destroy_all()
self.destroy_top()
self.show_items(doc)
def has_config_changed(self):
"""
Checks if the configuration has been changed since last loading
:return: Boolean
"""
return self.lastinit != os.path.getmtime(self.path + '/pimenu.yaml')
def show_items(self, items, upper=None):
"""
Creates a new page on the stack, automatically adds a back button when there are
pages on the stack already
:param items: list the items to display
:param upper: list previous levels' ids
:return: None
"""
if upper is None:
upper = []
num = 0
# create a new frame
wrap = Frame(self, bg="black")
if len(self.framestack):
# when there were previous frames, hide the top one and add a back button for the new one
self.hide_top()
back = FlatButton(
wrap,
text='back…',
image=self.get_icon("arrow.left"),
command=self.go_back,
)
back.set_color("#00a300") # green
back.grid(row=0, column=0, padx=1, pady=1, sticky=TkC.W + TkC.E + TkC.N + TkC.S)
num += 1
# add the new frame to the stack and display it
self.framestack.append(wrap)
self.show_top()
# calculate tile distribution
allitems = len(items) + num
rows = floor(sqrt(allitems))
cols = ceil(allitems / rows)
# make cells autoscale
for x in range(int(cols)):
wrap.columnconfigure(x, weight=1)
for y in range(int(rows)):
wrap.rowconfigure(y, weight=1)
# display all given buttons
for item in items:
act = upper + [item['name']]
if 'icon' in item:
image = self.get_icon(item['icon'])
else:
image = self.get_icon('scrabble.' + item['label'][0:1].lower())
btn = FlatButton(
wrap,
text=item['label'],
image=image
)
if 'items' in item:
# this is a deeper level
btn.configure(command=lambda act=act, item=item: self.show_items(item['items'], act),
text=item['label'] + '…')
btn.set_color("#2b5797") # dark-blue
else:
# this is an action
btn.configure(command=lambda act=act: self.go_action(act), )
if 'color' in item:
btn.set_color(item['color'])
# add buton to the grid
btn.grid(
row=int(floor(num / cols)),
column=int(num % cols),
padx=1,
pady=1,
sticky=TkC.W + TkC.E + TkC.N + TkC.S
)
num += 1
def get_icon(self, name):
"""
Loads the given icon and keeps a reference
:param name: string
:return:
"""
if name in self.icons:
return self.icons[name]
ico = self.path + '/ico/' + name + '.png'
if not os.path.isfile(ico):
ico = self.path + '/ico/' + name + '.gif'
if not os.path.isfile(ico):
ico = self.path + '/ico/cancel.gif'
self.icons[name] = PhotoImage(file=ico)
return self.icons[name]
def hide_top(self):
"""
hide the top page
:return:
"""
self.framestack[len(self.framestack) - 1].pack_forget()
def show_top(self):
"""
show the top page
:return:
"""
self.framestack[len(self.framestack) - 1].pack(fill=TkC.BOTH, expand=1)
def destroy_top(self):
"""
destroy the top page
:return:
"""
self.framestack[len(self.framestack) - 1].destroy()
self.framestack.pop()
def destroy_all(self):
"""
destroy all pages except the first aka. go back to start
:return:
"""
while len(self.framestack) > 1:
self.destroy_top()
def go_action(self, actions):
"""
execute the action script
:param actions:
:return:
"""
# hide the menu and show a delay screen
self.hide_top()
delay = Frame(self, bg="#2d89ef")
delay.pack(fill=TkC.BOTH, expand=1)
label = Label(delay, text="Executing...", fg="white", bg="#2d89ef", font="Sans 30")
label.pack(fill=TkC.BOTH, expand=1)
self.parent.update()
# excute shell script
subprocess.call([self.path + '/pimenu.sh'] + actions)
# remove delay screen and show menu again
delay.destroy()
self.destroy_all()
self.show_top()
def go_back(self):
"""
destroy the current frame and reshow the one below, except when the config has changed
then reinitialize everything
:return:
"""
if self.has_config_changed():
self.initialize()
else:
self.destroy_top()
self.show_top()
def main():
root = Tk()
root.geometry("320x240")
root.wm_title('PiMenu')
if len(sys.argv) > 1 and sys.argv[1] == 'fs':
root.wm_attributes('-fullscreen', True)
PiMenu(root)
root.mainloop()
if __name__ == '__main__':
main()