/
kivy_app.py
830 lines (637 loc) · 32.9 KB
/
kivy_app.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
"""A platform to host the service of finding nearby live music
Simple, modern app. Uses the get_all_events() function to
return the bands playing that day or in the near future in Malahide.
All possible arguments for *args and **kwargs for each usage can be found in the kivy documentation at:
https://kivy.org/docs/api-kivy.html
"""
import json
from threading import Thread
import _mysql_exceptions
from kivy.animation import Animation
from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.core.window import Window
from kivy.effects.opacityscroll import OpacityScrollEffect
from kivy.graphics import Rectangle, Color, Canvas
from kivy.graphics.context_instructions import PushMatrix, PopMatrix, Rotate
from kivy.properties import NumericProperty
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.scrollview import ScrollView
from kivy.uix.textinput import TextInput
from kivy.utils import escape_markup
import sys
import os
sys.path.append(os.path.dirname(__file__)+"\\src")
from reach_db import get_db_info
def search_events(searched_value, searched_type, all_events):
"""
Takes the text searched in a search bar and finds all events with any information matching the search
:param searched_value: the text searched by the user
:type: str
:param searched_type: the instance of MyTextInput with the screen that it belongs to
:type: object
:param all_events: all events taken from the database
:type: list
:return: the events from all events that have information on them that matches the search
:rtype: list
"""
base_events = all_events
new_future_events = list()
new_today_events = list()
event_nearness = 0 if str(searched_type) == 'MyTextInput_today results' else 1
list_destination = new_today_events if event_nearness == 0 else new_future_events
for info_list in base_events[event_nearness]:
for event_property_list in info_list:
for event_property in event_property_list:
if searched_value.lower() in event_property.lower():
list_destination.append(info_list)
break
if info_list in list_destination:
break
return [new_today_events, new_future_events, []]
def update_live_music_events():
"""
Allows me to update the variable live_music_events anywhere in file with error handling
:return: the updated live_music_events
:rtype: list
"""
try:
new_live_music_events = get_db_info()
except _mysql_exceptions.OperationalError:
new_live_music_events = [
[[('Whoops! No internet connection!', 'Use the refresh button to try again!')]] for i in range(3)
]
return new_live_music_events
class MyLoader(FloatLayout):
"""
This class contains the methods necessary to rotate an image with kivy. To be used for all the loading animations
which entail using rotating arrows.
"""
angle = NumericProperty(0)
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.uix.floatlayout.html
"""
super().__init__(**kwargs)
self.root = root = self
self.root.canvas = Canvas()
root.bind(angle=self.on_angle)
self.rotate_animation = Animation(angle=0.5, duration=1)
self.rotate_animation.start(self)
def on_angle(self, item, angle):
"""
Every time the angle of the item changes, this function is called to continue the rotation. Almost like a
recursive function as each change in angle runs this which causes another change.
:param item: the object being rotated
:type: object
:param angle: the angle at which the item's rotation will change to
:type: int
"""
item.rotate_animation.stop(self)
item.rotate_animation = Animation(angle=1, duration=0.00001)
item.rotate_animation.start(self)
if int(angle * 360) == 360:
item.rotate_animation += Animation(angle=angle + 1, duration=0.00001)
item.rotate_animation.start(self)
with self.root.canvas.before:
PushMatrix()
Rotate(angle=angle, axis=(0, 0, 1), origin=self.root.center)
with self.root.canvas.after:
PopMatrix()
class MyTextInput(TextInput):
"""
A search bar for today's events and future's events. Each time text updates, updates the scrolling view.
"""
def __init__(self, event_screen, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param event_screen: the screen that the text input is held in
:type: object
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.uix.textinput.html
"""
super().__init__(**kwargs)
self.background_normal = 'Images/buttons/small_button.png'
self.background_active = 'Images/buttons/small_button_down.png'
self.font_name = font_path
self.foreground_color = (0.66, 0.66, 0.66, 1)
self.selection_color = (1, 0.23, 0.25, 0.6)
self.cursor_color = (1, 0.23, 0.25, 1)
self.hint_text = 'search'
self.font_size = 15
self.size = (67.5, 32)
self.size_hint = (None, None)
self.hint_text_color = (0.66, 0.66, 0.66, 1)
self.multiline = False
self.padding = [10, 7, 0, 7]
self.parent_screen = event_screen
self.text = last_today_search if self.parent_screen.name == 'today results' else last_future_search
def __str__(self):
"""
Simple change which helps the search_events function above
:return: a simple name to represent an instance
:rtype: str
"""
try:
return "MyTextInput_%s" % self.parent_screen.name
except AttributeError:
return "MyTextInput"
class MyLabel(Label):
"""
Sets the colour of the text and font of text to avoid repeating lines for each label (they are all the same)
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.uix.label.html
"""
super().__init__(**kwargs)
self.color = (0.66, 0.66, 0.66, 1)
self.font_name = font_path
class MyScrollingView(ScrollView):
"""
A scrollable label for both today's results screen and future's results screen
"""
def __init__(self, live_music_num, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param live_music_num: 0 signifies only today's events, while 1 signifies all foreseeable events, while 2
signifies past events
:type: int
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.uix.scrollview.html
"""
super().__init__(**kwargs)
self.live_music_num = live_music_num
self.effect_cls = OpacityScrollEffect
self.size = (230, 310)
self.size_hint = (1, None)
self.layout_display_results = GridLayout(cols=1, spacing=25, size_hint_y=None, padding=[10, 70, 10, 20])
self.layout_display_results.bind(minimum_height=self.layout_display_results.setter('height'))
self._output_string = ''
self.update_event_info()
self.add_widget(self.layout_display_results)
def update_event_info(self, specific_events=None):
"""
This updates the event info in the scrolling view.
:param specific_events: a set of events to be specified. Upon no input, will use all found events.
:type: list
"""
updated_live_music_events = specific_events if specific_events else update_live_music_events()
self.layout_display_results.clear_widgets()
if len(updated_live_music_events[self.live_music_num]) >= 1:
for info_list in updated_live_music_events[self.live_music_num]:
self._output_string = ''
combined_event_info_list = list()
for event_property_list in info_list:
combined_event_info_list.extend(event_property_list)
if len(combined_event_info_list) != 2:
event_date = combined_event_info_list[0]
event_time = combined_event_info_list[1]
event_cost = combined_event_info_list[2]
event_artist = combined_event_info_list[3]
event_venue = combined_event_info_list[5]
event_address = combined_event_info_list[6]
event_url = combined_event_info_list[7]
for event_detail in [event_artist, event_date, event_time, event_cost, event_venue,
event_address, event_url]:
cleaned_event_detail = str(event_detail).lower().strip().title()
if cleaned_event_detail != '-' and cleaned_event_detail not in self._output_string:
self._output_string += "{} \n".format(cleaned_event_detail)
artist = self._output_string.split('\n')[0]
if info_list != updated_live_music_events[self.live_music_num][-1]:
self._output_string += '------------------------------------'
label_results = MyLabel(
text='[font=Font/Raleway-SemiBold.ttf]' +
escape_markup(artist) + '[/font]' +
self._output_string[len(artist)::],
markup=True, text_size=[Window.width - 10, None], size_hint_y=None, font_size=10,
halign='center'
)
self.layout_display_results.add_widget(label_results)
else:
for sentence in combined_event_info_list:
if sentence not in self._output_string:
self._output_string += sentence + ' '
label_results = MyLabel(text=self._output_string, text_size=[Window.width - 10, None],
halign='center')
self.layout_display_results.add_widget(label_results)
else:
self._output_string = 'I am sorry, \nI could not find any events!'
label_results = MyLabel(text=self._output_string, text_size=[Window.width - 10, None], halign='center')
self.layout_display_results.add_widget(label_results)
if self.parent is not None:
Animation(opacity=1, duration=0.1).start(self.parent.base_scroll_view)
@classmethod
def new_tonights_results(cls):
"""
Eliminates the need to awkwardly remember that a zero denotes tonight's events
:return: an instance of MyScrollingView with results set to tonight
:rtype: object
"""
return cls(0)
@classmethod
def new_future_results(cls):
"""
Eliminates the need to awkwardly remember that a one denotes future events
:return: an instance of MyScrollingView with results set to future
:rtype: object
"""
return cls(1)
class MyScreen(Screen):
"""
All of the screens need the get_screen_manager method, therefore I created this object to hold the
existing methods and variables that a regular Screen needs and also the get_screen_manager method.
In addition, it draws and updates the background image on each screen.
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.modules.screen.html
"""
super().__init__(**kwargs)
self.button_to_home = MyButton('travel to home', 'home screen', 'down')
self.layout_text_input = AnchorLayout(anchor_x='center', anchor_y='top', padding=[11, 93, 11, 299])
self.layout_back_search = AnchorLayout(anchor_x='center', anchor_y='top', padding=[0, 5, 32, 5])
self.layout_inner_grid = GridLayout(cols=2, height=32, size_hint=(None, None), col_default_width=65, spacing=3)
self.layout_back_search.add_widget(self.layout_inner_grid)
self.layout_hidden = AnchorLayout(anchor_x='center', anchor_y='center', opacity=0)
self.searching_animation = Animation(opacity=0.58, duration=0.15) + Animation(opacity=0, duration=0.15)
self.opacity = 0
self.instance, self.value = None, None
self.root = root = self
root.bind(size=self._update_rect, pos=self._update_rect)
with root.canvas.before:
Color(0.93, 0.93, 0.93, 1)
self.rect = Rectangle(size=root.size, pos=root.pos)
def fill_screen(self):
"""
A simple function just used by the Future and Tonight events screens. Gets all the common widgets and adds them
to their respective layouts.
"""
self.layout_inner_grid.add_widget(self.button_to_home)
self.layout_hidden.add_widget(Image(source='Images/icons/search_icon.png'))
self.add_widget(self.layout_back_search)
self.add_widget(self.base_scroll_view)
self.add_widget(self.layout_hidden)
def _update_rect(self, *args):
"""
This must be in every screen as it is what fills the background dynamically.
:param args: the 0th element should have a pos and size.
"""
self.rect.pos = args[0].pos
self.rect.size = args[0].size
@staticmethod
def update_events_with_search(instance, value):
"""
Once the list of all events has been reduced using the user's search, the specific events are passed on to the
update_event_info method of MyScrollingView to update what the user sees.
:param instance: the instance of MyTextInput that the user is searching from.
:type: object
:param value: the text that the user has searched in the MyTextInput
:type: str
"""
specific_events = search_events(value, instance, live_music_events)
input_screen_name = str(instance).split('_')[1]
update_event_info_thread = Thread(
target=main_app.app_screen_manager.get_screen(input_screen_name).base_scroll_view.update_event_info,
args=(specific_events,)
)
update_event_info_thread.start()
while update_event_info_thread.is_alive():
pass
def callback(self, *args):
"""
Allows me to pass arguments to a function that will be called through .bind (as you see in the following method)
:param args: the args that will be passed on to the function
"""
self.update_events_with_search(self.instance, self.value)
def on_text(self, instance, value):
"""
This function is key as it actually starts the event searching process by calling the first function required,
update_events_with_search.
:param instance: the MyTextInput instance that was used to search
:type: object
:param value: the value of the search
:type: str
"""
self.instance = instance
self.value = value
self.searching_animation.start(self.layout_hidden)
fading_animation = Animation(opacity=0, duration=0.1) + Animation()
fading_animation.bind(on_complete=lambda *args: self.callback(*args))
fading_animation.start(self.base_scroll_view)
class MyButton(Button):
"""
Takes everything needed from the built-in Button class and adds in my own animation.
Shrinks both the button and font-size slightly and then reverts back to the original size.
"""
def __init__(self, button_usage, destination=None, t_direction=None, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param button_usage: the shortest description of what the button will do upon press
:type: str
:param destination: gives the screen name that the button will change to
:type: str
:param t_direction: transition direction of the screen- which direction does it slide, up or down
:type: str
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.uix.button.html
"""
super().__init__(**kwargs)
all_buttons.append(self)
self._usage = button_usage
self._manager = main_app.app_screen_manager
self._destination = destination
self._transition_direction = t_direction
self._last_position = 0
self.size_hint = (None, None)
if self._usage == 'travel to home':
self.background_normal = 'Images/buttons/small_button_arrow_up.png'
self.background_down = 'Images/buttons/small_button_arrow_down.png'
elif self._usage == 'music_change':
if background_music.state == 'stop':
self.background_normal = 'Images/buttons/small_button_volume_off.png'
elif background_music.state == 'play':
self.background_normal = 'Images/buttons/small_button_volume_on.png'
self.background_down = 'Images/buttons/small_button_volume_mid.png'
elif self._usage == 'refresh':
self.background_normal = 'Images/buttons/small_button_refresh_up.png'
self.background_down = 'Images/buttons/small_button_refresh_down.png'
elif self._usage == 'travel':
self.background_normal = 'Images/buttons/large_button.png'
self.background_down = 'Images/buttons/large_button_down.png'
else:
self.background_normal = 'Images/buttons/small_button.png'
self.background_down = 'Images/buttons/small_button_down.png'
self.border = (0, 0, 0, 0)
self.font_name = font_path
self.size = (140, 32) if self._usage == 'travel' else (67.5, 32)
self.music_setting = True if last_setting is True else False
self.color = (0.66, 0.66, 0.66, 1)
def update_scrolling_view(self, *args):
"""
This causes an update to the scrolling view in a thread- this was done so that I can tell from outside the
function (while it's running) whether or not it is complete. This was necessary for animation purposes. This
is different from previous methods that update the scrolling view as this is called when the refresh button is
pressed.
:param args: the function is called through a .bind, so parameters are passed that are not needed by default.
This parameter allows the function to work no matter what Kivy automatically passes it.
"""
update_event_info_today_thread = Thread(
target=self._manager.get_screen('today results').base_scroll_view.update_event_info
)
update_event_info_future_thread = Thread(
target=self._manager.get_screen('future results').base_scroll_view.update_event_info
)
update_event_info_today_thread.start()
update_event_info_future_thread.start()
while update_event_info_today_thread.is_alive() or update_event_info_future_thread.is_alive():
pass
out_fading_animation = Animation(opacity=1, duration=1.0)
hiding_animation = Animation(opacity=0, duration=0.5)
out_fading_animation.start(self.parent.parent.parent)
hiding_animation.start(self.parent.parent.parent.children[0].children[0].children[0])
def changer(self, *args):
"""
Depending on the instance of MyButton that was pressed, an action is taken.
:param args: the function is called through a .bind, so parameters are passed that are not needed by default.
This parameter allows the function to work no matter what Kivy automatically passes it.
"""
if 'travel' in self._usage:
darkening_animation.start(self._manager.current_screen)
lighting_animation.start(self._manager.get_screen(self._destination))
self._manager.current = self._destination
self._manager.transition.direction = self._transition_direction
elif self._usage == 'refresh':
fading_animation = Animation(opacity=0.6, duration=0.9) + Animation(duration=0)
revealing_animation = Animation(opacity=1, duration=0.4)
fading_animation.bind(on_complete=self.update_scrolling_view)
fading_animation.start(self.parent.parent.parent)
revealing_animation.start(self.parent.parent.parent.children[0].children[0].children[0])
elif self._usage == 'music_change':
if background_music.state == 'stop':
self.background_normal = 'Images/buttons/small_button_volume_on.png'
background_music.play()
background_music.seek(self._last_position)
self._volume_changer(True)
elif background_music.state == 'play':
self.background_normal = 'Images/buttons/small_button_volume_off.png'
self._last_position = background_music.get_pos()
background_music.stop()
self._volume_changer(False)
def on_press(self):
"""
This is called every time any instance is pressed. All it does is run the animation for the button to make it
look more like a real button and activate the music if that is what the default user setting calls for.
"""
button_animation = Animation(size=(self.size[0] - 10, self.size[1] - 5), duration=0.04) & \
Animation(font_size=7, duration=0.04) + \
Animation(size=(self.size[0], self.size[1]), duration=0.04) & \
Animation(font_size=self.font_size, duration=0.04)
button_animation.bind(on_complete=self.changer)
button_animation.start(self)
if self.music_setting is True:
button_sound.play()
@classmethod
def travel(cls, destination, t_direction, **kwargs):
"""
Allows me to not have to worry about entering the correct string while trying to create a button meant for
traveling to different screens.
:param destination: the button screen destination
:type: str
:param t_direction: the transition direction
:type: str
:param kwargs: other kwargs available at https://kivy.org/docs/api-kivy.uix.button.html
:return: an instance of MyButton that is set up for travel.
:rtype: object
"""
return cls('travel', destination, t_direction, **kwargs)
@staticmethod
def _volume_changer(new_music_setting):
"""
When the music button is pressed, it iterates through all buttons to either reactivate or deactivate their sound
:param new_music_setting: whether or not the music is on or off.
:type: boolean
"""
for button in all_buttons:
button.music_setting = new_music_setting
class HomeScreen(MyScreen):
"""
The main screen of the app
Holds both buttons to tonight's events page and to future's events page
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.modules.screen.html
"""
super().__init__(**kwargs)
_title_image = Image(source='Images/best_title_img.png')
layout_today_events = AnchorLayout(anchor_x='center', anchor_y='bottom', padding=[5, -500])
layout_future_events = AnchorLayout(anchor_x='center', anchor_y='bottom', padding=[5, -500])
layout_about = AnchorLayout(anchor_x='center', anchor_y='bottom', padding=[5, -500])
layout_title = AnchorLayout(anchor_x='center', anchor_y='top', padding=[5, -350, 5, 350])
layout_music_refresh = AnchorLayout(anchor_x='center', anchor_y='bottom', padding=[0, -500, 35, -500])
layout_inner_grid = GridLayout(cols=2, height=32, size_hint=(None, None), col_default_width=65, spacing=3)
layout_hidden_loading = AnchorLayout(anchor_x='center', anchor_y='center')
button_refresh = MyButton('refresh')
button_music = MyButton('music_change')
button_today_events = MyButton.travel('today results', 'up', text="tonight's beat")
button_future_events = MyButton.travel('future results', 'up', text="future's beat")
button_about = MyButton.travel('about screen', 'up', text='about')
image_hidden_loading = Image(size_hint=[None, None], source='Images/icons/arrow.png',
pos_hint={'center_x': 0.5, 'center_y': 0.5}, opacity=0)
rotating_hidden_layout = MyLoader()
rotating_hidden_layout.add_widget(image_hidden_loading)
for layout, widget in zip([layout_inner_grid, layout_inner_grid, layout_music_refresh, layout_future_events,
layout_today_events, layout_about, layout_title, layout_hidden_loading],
[button_refresh, button_music, layout_inner_grid, button_future_events,
button_today_events, button_about, _title_image,
rotating_hidden_layout]):
layout.add_widget(widget)
for layout in [layout_about, layout_future_events, layout_today_events,
layout_music_refresh, layout_title, layout_hidden_loading]:
self.add_widget(layout)
animation_future = Animation(pos=(0, 575), duration=1.9, t='out_quad')
animation_today = Animation(pos=(0, 609), duration=1.9, t='out_quad')
animation_about = Animation(pos=(0, 541), duration=1.9, t='out_quad')
animation_twin_button = Animation(pos=(0, 507), duration=1.9, t='out_quad')
animation_title = Animation(pos=(0, -265), duration=2.7, t='out_quad')
animation_lighting = Animation(opacity=1.0, duration=1.15)
for animation, layout in zip([animation_lighting, animation_twin_button, animation_about,
animation_future, animation_today, animation_title],
[self, layout_music_refresh, layout_about,
layout_future_events, layout_today_events, layout_title]):
animation.start(layout)
class TonightResultsScreen(MyScreen):
"""
Displays the results from get_events_malahide() that are on today
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.modules.screen.html
"""
super().__init__(**kwargs)
self.base_scroll_view = MyScrollingView.new_tonights_results()
today_text_input = MyTextInput(self)
today_text_input.bind(text=self.on_text)
self.layout_inner_grid.add_widget(today_text_input)
self.fill_screen()
class FutureResultsScreen(MyScreen):
"""
Displays all the results from get_events_malahide() that are on in the future
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.modules.screen.html
"""
super().__init__(**kwargs)
self.base_scroll_view = MyScrollingView.new_future_results()
future_text_input = MyTextInput(self)
future_text_input.bind(text=self.on_text)
self.layout_inner_grid.add_widget(future_text_input)
self.fill_screen()
class AboutScreen(MyScreen):
"""
Displays information about app and its usage
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.modules.screen.html
"""
super().__init__(**kwargs)
layout_display_results = BoxLayout(orientation='vertical', spacing=5, padding=[5])
self._output_string = '''
Find The Beat allows it\'s users to locate live music playing in their area,
while providing them with the key information to attend the event.
The art for the app was created by Robert Morgan.
The sound effects/music was created by Brandon Duffield.
The idea for the app was provided by Jon Czernel.
The web scraping functionality and the app itself was created by Logan Czernel.
Built with Python. Modules: Kivy, BeautifulSoup, MySQLdb and Requests.
'''
label_results = MyLabel(text=self._output_string, font_size=10, text_size=[200, 200], halign='center')
self.layout_to_home_screen = AnchorLayout(anchor_x='center', anchor_y='top', padding=[5])
for page_item, widget in zip([self.layout_to_home_screen, layout_display_results, self, self],
[self.button_to_home, label_results, layout_display_results,
self.layout_to_home_screen]):
page_item.add_widget(widget)
class MyApp(App):
"""
Set everything up, e.g., display the background.jpg as the background image.
Adjust the size of the image based on the size of the screen
"""
def __init__(self, **kwargs):
"""
Set up inheritance of parent with the kwargs as well as add variables needed by all instances.
:param kwargs: all available kwargs can be found at https://kivy.org/docs/api-kivy.app.html
"""
super().__init__(**kwargs)
self._app_screen_manager = ScreenManager(transition=SlideTransition(duration=0.25))
def build(self):
"""
Function required in Kivy- builds the app.
:return: the app screen manager
:rtype: object
"""
home_screen = HomeScreen(name='home screen')
today_results_screen = TonightResultsScreen(name='today results')
future_results_screen = FutureResultsScreen(name='future results')
about_screen = AboutScreen(name='about screen')
for app_screen in [home_screen, today_results_screen, future_results_screen, about_screen]:
self._app_screen_manager.add_widget(app_screen)
self.title = 'Find The Beat'
return self._app_screen_manager
@property
def app_screen_manager(self):
return self._app_screen_manager
if __name__ == '__main__':
# create common animations
darkening_animation = Animation(opacity=0, duration=0.5)
lighting_animation = Animation(opacity=1.0, duration=0.5)
# get the live music events
live_music_events = update_live_music_events()
# create list to hold all the MyButton instances
all_buttons = []
# save the file path to the font used in a variable
font_path = 'Font/Raleway-Regular.ttf'
# load the app sounds
background_music = SoundLoader.load('Sounds/backgroundmusic.wav')
button_sound = SoundLoader.load('Sounds/buttonsound.wav')
# open the user_settings.json file and get the user settings
with open('user_settings.json') as fs:
json_data = json.load(fs)
last_setting = json_data.get('music_active')
previous_searches = json_data.get('user_searches')
last_today_search = previous_searches.get('search_today')
last_future_search = previous_searches.get('search_future')
# lower button sound
if button_sound:
button_sound.volume = 0.1
# loop background music, lower it's volume and if the last music setting is true, play the music
if background_music:
background_music.loop = True
background_music.volume = 0.5
if last_setting is True:
background_music.play()
# build and run the app
main_app = MyApp()
main_app.run()
# upon closing the app, access user_settings.json and write the previous settings that were changed during last run
with open('user_settings.json', 'w') as fs:
music_data = main_app.app_screen_manager.get_screen('home screen').children[4].children[0].music_setting
last_today_text = main_app.app_screen_manager.get_screen('today results').children[2].children[0].children[
1].text
last_future_text = main_app.app_screen_manager.get_screen('future results').children[2].children[0].children[
1].text
json.dump({"music_active": music_data,
"user_searches": {"search_today": last_today_text, "search_future": last_future_text}}, fs)