-
Notifications
You must be signed in to change notification settings - Fork 1
/
pitkgui.py
executable file
·397 lines (353 loc) · 8.93 KB
/
pitkgui.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python
import sys, os, time, socket
from Tkinter import *
from tkFont import Font
from subprocess import *
# Get Your External IP Address
def get_ip():
ip_msg = "Not connected"
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.connect(('<broadcast>', 0))
ip_msg = s.getsockname()[0]
except Exception:
pass
return ip_msg
# Startx
def startx():
command = "/usr/bin/startx"
process = Popen(command.split(), stdout=PIPE)
output = process.communicate()[0]
return output
# Restart Raspberry Pi
def restart():
command = "/usr/bin/sudo /sbin/shutdown -r now"
process = Popen(command.split(), stdout=PIPE)
output = process.communicate()[0]
return output
# Shutdown Raspberry Pi
def shutdown():
command = "/usr/bin/sudo /sbin/shutdown -h now"
process = Popen(command.split(), stdout=PIPE)
output = process.communicate()[0]
return output
# Get time and date
def get_date():
d = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
return d
def get_temp():
command = "vcgencmd measure_temp"
process = Popen(command.split(), stdout=PIPE)
output = process.communicate()[0]
temp = output[5:-1]
return temp
def get_clock():
command = "vcgencmd measure_clock arm"
process = Popen(command.split(), stdout=PIPE)
output = process.communicate()[0]
clock = output.split("=")
clock = int(clock[1][:-1]) / 1024 /1024
clock = str(clock) + "MHz"
return clock
def get_volts():
command = "vcgencmd measure_volts"
process = Popen(command.split(), stdout=PIPE)
output = process.communicate()[0]
volts = output[5:-1]
return volts
class PitkGui:
def __init__(self, parent):
#--- main frame
main_bg = "black"
main_fg = "white"
#--- labels
labFont = Font(family="Courier", size=11)
labFont.configure(weight = "bold")
#--- pulsanti
butFont = Font(family="Helvetica", size=11)
butFont.configure(weight = "bold")
but_width = 10
but_height = 0
but_padx = "3m"
but_pady = "1m"
but_relief = FLAT
but_borderwidth = 1
but_hlcol = "red"
but_hlbg = "red"
but_bg = "#8B0000"
but_fg = "white"
but_actbg = "red"
but_actfg = "white"
#--------------------- fine costanti ----------------
self.myParent = parent
#--------------------- FRAMES -----------------------
### Il quadro principale si chiama 'myBox1'
self.myBox1 = Frame(parent,
background=main_bg,
)
self.myBox1.pack(side = TOP,
fill = BOTH,
expand = YES,
)
# Il quadro dei pulsanti alto
self.button_square = Frame(self.myBox1,
background=main_bg,
#relief=RAISED, borderwidth=1
)
self.button_square.pack(side = TOP,
padx=5,
pady=5,
fill = BOTH,
expand = YES,
)
# quadro centrale
self.central_square = Frame(self.myBox1,
background=main_bg,
relief=FLAT, borderwidth=1,
padx = 5,
pady = 5,
)
self.central_square.pack(side = TOP,
fill = BOTH,
expand = YES,
ipadx = 5,
ipady = 5,
)
# Il quadro dei pulsanti basso
self.bottom_square = Frame(self.myBox1,
padx=5,
pady=5,
background=main_bg,
#relief=RAISED, borderwidth=1
)
self.bottom_square.pack(side = TOP,
fill = BOTH,
expand = YES,
)
# quadro sinistra
self.left_square = Frame(self.central_square,
background=main_bg,
#borderwidth = 5,
#relief = RIDGE,
)
self.left_square.pack(side = LEFT,
fill = BOTH,
expand = YES,
)
# quadro destra
self.right_square = Frame(self.central_square,
background=main_bg,
#borderwidth = 5,
#relief = RIDGE,
)
self.right_square.pack(side = RIGHT,
fill = BOTH,
expand = YES,
)
#--------------------- FIXED LABELS -----------------------
# etichette sinistra
# IP
self.label1 = Label(self.left_square,
background=main_bg, foreground=main_fg,
text="IP: ",
anchor='w',
font=labFont,
)
self.label1.pack(
fill=X,
expand = YES
)
# Temp
self.label2 = Label(self.left_square,
background=main_bg, foreground=main_fg,
text="Temp: ",
anchor='w',
font=labFont,
)
self.label2.pack(
fill=X,
expand = YES
)
# Date
self.label3 = Label(self.left_square,
background=main_bg, foreground=main_fg,
text="Date: ",
anchor='w',
font=labFont,
)
self.label3.pack(
fill=X,
expand = YES
)
# Volts
self.label4 = Label(self.left_square,
background=main_bg, foreground=main_fg,
text="Core: ",
anchor='w',
font=labFont,
)
self.label4.pack(
fill=X,
expand = YES
)
# Clock
self.label5 = Label(self.left_square,
background=main_bg, foreground=main_fg,
text="Clock: ",
anchor='w',
font=labFont,
)
self.label5.pack(
fill=X,
expand = YES
)
#--------------------- VAR LABELS -----------------------
# etichette destra
self.label1var = Label(self.right_square,
background=main_bg, foreground=main_fg,
text=get_ip(),
anchor='w',
font=labFont,
)
self.label1var.pack(
fill=X,
expand = YES
)
#var_temp = StringVar();
#var_temp.set(get_temp());
self.label2var = Label(self.right_square,
background=main_bg, foreground=main_fg,
#textvariable=var_temp,
anchor='w',
font=labFont,
)
self.label2var.pack(
fill=X,
expand = YES
)
#var_date = StringVar()
#var_date.set(get_date())
self.label3var = Label(self.right_square,
background=main_bg, foreground=main_fg,
# textvariable=var_date,
anchor='w',
font=labFont,
)
self.label3var.pack(
fill=X,
expand = YES
)
self.label4var = Label(self.right_square,
background=main_bg, foreground=main_fg,
text=get_volts(),
anchor='w',
font=labFont,
)
self.label4var.pack(
fill=X,
expand = YES
)
self.label5var = Label(self.right_square,
background=main_bg, foreground=main_fg,
text=get_clock(),
anchor='w',
font=labFont,
)
self.label5var.pack(
fill=X,
expand = YES
)
# Vengono ora aggiunti i pulsanti a 'button_square'
#--------------------- TOP BUTTONS -----------------------
self.pulsante1 = Button(self.button_square, command = self.buttonPress1)
self.pulsante1.configure(text = "STARTX")
self.pulsante1.configure(
width = but_width, height = but_height,
padx = but_padx, pady = but_pady,
relief = but_relief, borderwidth = but_borderwidth,
highlightbackground = but_hlbg, highlightcolor = but_hlcol,
activebackground = but_actbg, activeforeground = but_actfg,
background = but_bg, foreground = but_fg,
font=butFont,
)
self.pulsante1.pack(side = LEFT)
self.pulsante1.bind("<Return>", self.buttonPress1_a)
self.pulsante2 = Button(self.button_square, command = self.buttonPress2)
self.pulsante2.configure(text = "EXIT")
# default focus
self.pulsante2.focus_force()
self.pulsante2.configure(
width = but_width, height = but_height,
padx = but_padx, pady = but_pady,
relief = but_relief, borderwidth = but_borderwidth,
highlightbackground = but_hlbg, highlightcolor = but_hlcol,
activebackground = but_actbg, activeforeground = but_actfg,
background = but_bg, foreground = but_fg,
font=butFont,
)
self.pulsante2.pack(side = RIGHT)
self.pulsante2.bind("<Return>", self.buttonPress2_a)
#-------------------- BOTTOM BUTTONS ----------------------
self.pulsante3 = Button(self.bottom_square, command = self.buttonPress3)
self.pulsante3.configure(text = "REBOOT")
self.pulsante3.configure(
width = but_width, height = but_height,
padx = but_padx, pady = but_pady,
relief = but_relief, borderwidth = but_borderwidth,
highlightbackground = but_hlbg, highlightcolor = but_hlcol,
activebackground = but_actbg, activeforeground = but_actfg,
background = but_bg, foreground = but_fg,
font=butFont,
)
self.pulsante3.pack(side = LEFT)
self.pulsante3.bind("<Return>", self.buttonPress3_a)
self.pulsante4 = Button(self.bottom_square, command = self.buttonPress4)
self.pulsante4.configure(text = "SHUTDOWN")
self.pulsante4.configure(
width = but_width, height = but_height,
padx = but_padx, pady = but_pady,
relief = but_relief, borderwidth = but_borderwidth,
highlightbackground = but_hlbg, highlightcolor = but_hlcol,
activebackground = but_actbg, activeforeground = but_actfg,
background = but_bg, foreground = but_fg,
font=butFont,
)
self.pulsante4.pack(side = RIGHT)
self.pulsante4.bind("<Return>", self.buttonPress4_a)
def buttonPress1(self):
startx();
def buttonPress2(self):
self.myParent.destroy()
def buttonPress3(self):
restart();
def buttonPress4(self):
shutdown();
# event management (not really used now)
def buttonPress1_a(self, event):
self.buttonPress1()
def buttonPress2_a(self, event):
self.buttonPress2()
def buttonPress3_a(self, event):
self.buttonPress3()
def buttonPress4_a(self, event):
self.buttonPress4()
# REFRESH VARS
def do_refresh(self, date, temp):
var_date = StringVar()
var_date.set(date)
self.label3var.configure(textvariable=var_date);
var_temp = StringVar();
var_temp.set(temp);
self.label2var.configure(textvariable=var_temp);
# redraw
root.update_idletasks();
# MAIN
root = Tk()
root.geometry("320x240")
pitkGui = PitkGui(root)
def task():
pitkGui.do_refresh(get_date(), get_temp())
root.after(1000, task)
root.after(0, task)
root.mainloop()