This repository was archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStageDisplay.py
More file actions
558 lines (442 loc) · 18.2 KB
/
StageDisplay.py
File metadata and controls
558 lines (442 loc) · 18.2 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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
"ProPresenter Stage Display App - Implements Clock, Current & Next Text"
import os
import json
from ProPresenterStageDisplayClientComms import ProPresenterStageDisplayClientComms
import Tkinter as tk
import time
__author__ = "Anthony Eden"
__copyright__ = "Copyright 2017-2018, Anthony Eden / Media Realm"
__credits__ = ["Anthony Eden"]
__license__ = "GPL"
__version__ = "1.0"
class Application(tk.Frame):
# Store the Tk root class
root = None
# Store the threaded class for ProPresenter
ProPresenter = None
# Config data for ProPresenter
ProP_IPAddress = None
ProP_IPPort = None
ProP_Password = None
# Store the labels on the screen
labelCurrent = None
labelNext = None
labelClock = None
labelTimer = None
# Store the last time string
time_last = ""
# The maximum length of each line on the screen - set in config file
wordWrapLength = 1500
# Font sizes
fontSizeClock = 52
fontSizeCurrent = 52
fontSizeNext = 36
# Font name
fontName = "Arial"
fontStyle = "bold"
fontUppercase = False
fontAlign = tk.S + tk.W + tk.E
fontJustify = tk.CENTER
# Lower Third Mode?
modeLowerThird = False
# Merge every 2nd line (for lower thirds)
mergeLines = False
mergeLinesMin = 4
mergeLinesJoinChar = ","
mergeLinesStripTrailing = [".", ";", ",", " "]
# Allow splitting the text at a certain delimiter
splitLinesChar = None
# Specify the padding for the screen
padX = 50
padY = 50
# Allow user-configurable background and text colours
backgroundColour = "black"
textColour = "white"
# Store the next that needs to be rendered
currentText = ""
nextText = ""
# Store a dict of all currently running timers, so we can work out which ones to show
runningTimers = {}
timerRank = []
# Lower third padding
lowerThirdHeight = 0
lowerThirdContainer = None
# Do we need to attempt a reconnection?
tryReconnect = False
disconnectTime = 0
def __init__(self, master = None):
# Setup the application and display window
self.root = tk.Tk()
self.root.protocol("WM_DELETE_WINDOW", self.close)
self.root.focus_set()
self.root.attributes('-fullscreen', True)
self.root.bind('<KeyPress>', self.close)
self.root.config(cursor = 'none')
# Get the config from JSON
try:
ConfigData_Filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json")
ConfigData_JSON = open(ConfigData_Filename).read()
ConfigData = json.loads(ConfigData_JSON)
except Exception, e:
print
print "##############################################"
print "EXCEPTION: Cannot load and parse Config.JSON File: "
print e
print "##############################################"
print
exit()
try:
self.ProP_IPAddress = ConfigData['IPAddress']
self.ProP_IPPort = int(ConfigData['IPPort'])
self.ProP_Password = ConfigData['Password']
self.wordWrapLength = int(ConfigData['WordWrapLength'])
self.fontSizeClock = int(ConfigData['FontSizeClock'])
self.fontSizeCurrent = int(ConfigData['FontSizeCurrent'])
self.fontSizeNext = int(ConfigData['FontSizeNext'])
if "LowerThirdMode" in ConfigData and ConfigData['LowerThirdMode'] is True:
self.modeLowerThird = True
if "FontName" in ConfigData:
self.fontName = ConfigData['FontName']
if "FontStyle" in ConfigData:
self.fontStyle = ConfigData['FontStyle']
if "FontUppercase" in ConfigData and ConfigData['FontUppercase'] is True:
self.fontUppercase = True
if "FontAlign" in ConfigData and ConfigData['FontAlign'] == "center":
self.fontAlign = tk.S + tk.W + tk.E
self.fontJustify = tk.CENTER
if "FontAlign" in ConfigData and ConfigData['FontAlign'] == "left":
self.fontAlign = tk.S + tk.W
self.fontJustify = tk.LEFT
if "FontAlign" in ConfigData and ConfigData['FontAlign'] == "right":
self.fontAlign = tk.S + tk.E
self.fontJustify = tk.RIGHT
if "MergeLines" in ConfigData and ConfigData['MergeLines'] is True:
self.mergeLines = True
if "MergeLinesMin" in ConfigData:
self.mergeLinesMin = int(ConfigData['MergeLinesMin'])
if "MergeLinesJoinChar" in ConfigData:
self.mergeLinesJoinChar = ConfigData['MergeLinesJoinChar']
if "PadX" in ConfigData:
self.padX = int(ConfigData['PadX'])
if "PadY" in ConfigData:
self.padY = int(ConfigData['PadY'])
if "LowerThirdHeight" in ConfigData:
self.lowerThirdHeight = int(ConfigData['LowerThirdHeight'])
else:
self.lowerThirdHeight = self.root.winfo_screenheight() - (self.padY * 2)
if "BackgroundColour" in ConfigData:
self.backgroundColour = ConfigData['BackgroundColour']
if "TextColour" in ConfigData:
self.textColour = ConfigData['TextColour']
if "SplitLines" in ConfigData:
self.splitLinesChar = ConfigData['SplitLines']
if "TimerLabels" in ConfigData:
self.timerRank = ConfigData['TimerLabels']
except Exception, e:
print
print "##############################################"
print "EXCEPTION: Config file is missing a setting"
print e
print "##############################################"
print
exit()
if self.modeLowerThird:
self.setupMainInterface_LowerThird()
else:
self.setupMainInterface()
self.connect()
self.reconnect_tick()
def connect(self):
# Connect to ProPresenter and setup the necessary callbacks
self.tryReconnect = False
self.disconnectTime = 0
self.ProPresenter = ProPresenterStageDisplayClientComms(self.ProP_IPAddress, self.ProP_IPPort, self.ProP_Password)
self.ProPresenter.addSubscription("CurrentSlide", self.updateSlideTextCurrent)
self.ProPresenter.addSubscription("NextSlide", self.updateSlideTextNext)
self.ProPresenter.addSubscription("Connected", self.connected)
self.ProPresenter.addSubscription("ConnectionFailed", self.connectFailed)
self.ProPresenter.addSubscription("Disconnected", self.disconnected)
self.ProPresenter.addSubscription("Timer*", self.updateTimer)
self.ProPresenter.addSubscription("VideoCounter", self.updateTimerVideo)
self.ProPresenter.start()
def connected(self, data):
print "ProPresenter Connected"
def connectFailed(self, error):
self.tryReconnect = True
if self.disconnectTime == 0:
self.disconnectTime = time.time()
print "ProPresenter Connect Failed", error
def disconnected(self, error):
self.tryReconnect = True
if self.disconnectTime == 0:
self.disconnectTime = time.time()
print "ProPresenter Disconnected", error
def reconnect_tick(self):
if self.tryReconnect and self.disconnectTime < time.time() - 5:
print "Attempting to reconnect to ProPresenter"
self.connect()
self.labelCurrent.after(2000, self.reconnect_tick)
def setupMainInterface(self):
# Setup the interface widgets
# Setup the main window for the application
tk.Frame.__init__(
self,
None,
background = self.backgroundColour
)
self.grid(sticky = tk.N + tk.S + tk.E + tk.W)
self.top = self.winfo_toplevel()
self.rowconfigure(0, weight = 1)
self.rowconfigure(1, weight = 40)
self.rowconfigure(2, weight = 20)
self.columnconfigure(0, weight = 1)
self.columnconfigure(1, weight = 1)
self.top.grid()
self.top.rowconfigure(0, weight = 1)
self.top.columnconfigure(0, weight = 1)
self.grid_propagate(False)
self.top.grid_propagate(False)
# Clock Text Label
self.labelClock = tk.Label(
self,
text = str("Clock"),
font = (self.fontName, self.fontSizeClock, self.fontStyle),
background = self.backgroundColour,
foreground = self.textColour,
wraplength = self.wordWrapLength,
anchor = tk.NW,
justify = tk.LEFT
)
self.labelClock.grid(
column = 0,
row = 0,
sticky = tk.W+tk.N+tk.S,
padx = self.padX,
pady = self.padY
)
# Timer Text Label
self.labelTimer = tk.Label(
self,
text = "",
font = (self.fontName, self.fontSizeClock, self.fontStyle),
background = self.backgroundColour,
foreground = self.textColour,
wraplength = self.wordWrapLength,
anchor = tk.NE,
justify = tk.RIGHT
)
self.labelTimer.grid(
column = 1,
row = 0,
sticky = tk.E+tk.N+tk.S,
padx = self.padX,
pady = self.padY
)
# Current Slide Text Label
self.labelCurrent = tk.Label(
self,
text = "Waiting for data...",
font = (self.fontName, self.fontSizeCurrent, self.fontStyle),
background = self.backgroundColour,
foreground = self.textColour,
wraplength = self.wordWrapLength,
anchor = tk.NW,
justify = tk.LEFT,
)
self.labelCurrent.grid(
column = 0,
row = 1,
sticky = tk.W+tk.E+tk.N+tk.S,
padx = self.padX,
pady = self.padY,
columnspan = 2,
)
# Next Slide Text Label
self.labelNext = tk.Label(
self,
text = "",
font = (self.fontName, self.fontSizeNext, self.fontStyle),
background = self.backgroundColour,
foreground = self.textColour,
wraplength = self.wordWrapLength,
anchor = tk.NW,
justify = tk.LEFT,
)
self.labelNext.grid(
column = 0,
row = 2,
sticky = tk.W+tk.E+tk.N+tk.S,
padx = self.padX,
pady = self.padY,
columnspan = 2,
)
def setupMainInterface_LowerThird(self):
# Setup the interface widgets - for lower third mode
# Setup the main window for the application
tk.Frame.__init__(
self,
None,
background = self.backgroundColour,
cursor = "none"
)
self.grid(sticky = tk.N + tk.S + tk.E + tk.W)
self.top = self.winfo_toplevel()
self.rowconfigure(0, weight = 1)
self.columnconfigure(0, weight = 1)
self.top.grid()
self.top.rowconfigure(0, weight = 1)
self.top.columnconfigure(0, weight = 1)
self.lowerThirdContainer = tk.Frame(
self,
height = self.lowerThirdHeight,
width = self.root.winfo_screenwidth() - (self.padX * 2),
background = self.backgroundColour,
)
self.lowerThirdContainer.pack_propagate(0)
self.lowerThirdContainer.place(x = self.padX, y = self.root.winfo_screenheight() - self.padY - self.lowerThirdHeight)
self.labelCurrent = tk.Label(
self.lowerThirdContainer,
text = "Waiting for data...",
font = (self.fontName, self.fontSizeCurrent, self.fontStyle),
foreground = self.textColour,
background = self.backgroundColour,
wraplength = self.wordWrapLength,
justify = self.fontJustify,
)
if self.fontJustify == tk.RIGHT:
anchor = tk.E
elif self.fontJustify == tk.LEFT:
anchor = tk.W
else:
anchor = tk.CENTER
if self.lowerThirdHeight == self.root.winfo_screenheight() - (self.padY * 2):
# Anchor to bottom of screen - old behaviour is now the default
expand = 0
side = tk.BOTTOM
else:
# Centre align
expand = 1
side = tk.TOP
self.labelCurrent.pack(fill = tk.Y, expand = expand, anchor = anchor, side = side)
def updateSlideTextCurrent(self, data):
# Update the text label for the current slide
if self.labelCurrent is None:
return False
if data['text'] is None:
self.currentText = ""
return None
if self.splitLinesChar is not None and data['text'] is not None and self.splitLinesChar in data['text']:
data['text'] = data['text'].split(self.splitLinesChar)
data['text'] = data['text'][0]
if self.mergeLines:
# Prepare to remove every 2nd line break
lines = data['text'].encode('utf-8').split("\n")
textOutput = ""
if len(lines) < self.mergeLinesMin:
# No need to merge these lines
textOutput = "\n".join(lines)
else:
# Join every 2nd line to the previous
for i, line in enumerate(lines):
# Strip trailing or leading whitespace from each line
line = line.strip()
# Get rid of some trailing punctuation before the merge
if line[-1:] in self.mergeLinesStripTrailing:
line = line[:-1]
if i == 0:
textOutput = line
elif i % 2 == 0:
textOutput += "\n" + line
else:
textOutput += self.mergeLinesJoinChar.encode('utf-8') + " " + line
else:
textOutput = data['text'].encode('utf-8')
# We want the text to be updated by the main thread, not the ProPresenter thread
if self.fontUppercase:
self.currentText = textOutput.upper()
elif not self.fontUppercase:
self.currentText = textOutput
def updateSlideTextNext(self, data):
# Update the text label for the next slide
if self.labelNext is None:
return False
if self.splitLinesChar is not None and data['text'] is not None and self.splitLinesChar in data['text']:
data['text'] = data['text'].split(self.splitLinesChar)
data['text'] = data['text'][0]
# We want the text to be updated by the main thread, not the ProPresenter thread
if data['text'] is not None and self.fontUppercase:
self.nextText = data['text'].encode('utf-8').upper()
elif data['text'] is not None and not self.fontUppercase:
self.nextText = data['text'].encode('utf-8')
else:
self.nextText = ""
def updateTimer(self, data):
# Update the various timers
if self.labelTimer is None:
return False
if data['text'] is None or 'label' not in data:
return None
if data['text'] == "--:--:--":
data['running'] = "0"
if ('running' not in data or data['running'] == "0") and data['label'] in self.runningTimers:
del self.runningTimers[data['label']]
if 'overrun' not in data:
data['overrun'] = '0'
if 'running' in data and data['running'] == "1":
self.runningTimers[data['label']] = {
"current": data['text'],
"overrun": data['overrun'],
"type": data['type'],
}
if len(self.runningTimers) == 0:
self.labelTimer.config(text = "")
return None
# Loop over all configured timers, and show the highest priority one
for timerName in self.timerRank:
if timerName in self.runningTimers:
self.labelTimer.config(text = self.runningTimers[timerName]['current'])
if self.runningTimers[timerName]['overrun'] == "1" and self.runningTimers[timerName]['current'][:1] == "-":
self.labelTimer.config(foreground = "red")
else:
self.labelTimer.config(foreground = self.textColour)
return True
# Nothing to display - empty out the field
self.labelTimer.config(text = "")
def updateTimerVideo(self, data):
# Update the video countdown timer
if self.labelTimer is None:
return False
if data['text'] != '--:--:--':
data['running'] = '1'
self.updateTimer(data)
def close(self, extra = None):
# Terminate the application
if self.ProPresenter is not None:
self.ProPresenter.stop()
self.root.destroy()
def clock_tick(self):
# Sets the clock time
if self.labelClock is None:
return False
time_now = time.strftime('%I:%M:%S %p')
# Update the timer on screen when the timer has incremented
if time_now != self.time_last:
self.time_last = time_now
self.labelClock.config(text = time_now)
self.labelClock.after(200, self.clock_tick)
def updatetext_tick(self):
# Update text from the main thread (to try and avoid redraw issues)
if self.labelCurrent is not None:
self.labelCurrent.configure(text = self.currentText)
self.update_idletasks()
if self.labelNext is not None:
self.labelNext.configure(text = self.nextText)
self.update_idletasks()
if self.labelCurrent is not None:
self.labelCurrent.after(100, self.updatetext_tick)
if __name__ == "__main__":
app = Application()
app.master.title('ProPresenter Stage Display')
app.clock_tick()
app.updatetext_tick()
app.mainloop()