-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
1644 lines (1395 loc) · 68.7 KB
/
main.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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""A mobile educational application about electricity and magnetism."""
# Operating System functionality.
import os
# Shuffle answers in quizzes to confuse students.
import random
import sympy
# SQLite connections and retrieval.
import sqlite3
# Electrical circuits calculations.
#from dcelectricity.dc_en import *
# GUI
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.slider import Slider
from kivy.uix.dropdown import DropDown
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.uix.settings import SettingsWithTabbedPanel
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.rst import RstDocument
from kivy.uix.popup import Popup
# Layouts
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.logger import Logger
from kivy.base import EventLoop
# Animation
from kivy.animation import Animation
import kivy
kivy.require('1.11.1')
class MenuScreen(Screen):
"""
Custom Menu Screen.
It inherits from Screen.
"""
class MenuScreenButton(Button):
"""
Custom button style for the Menu Screen.
It inherits from kivy.uix.button.
"""
class PurpleRoundedButton(Button):
"""
Custom button style for the "back to menu" and "reset" buttons.
It inherits from kivy.uix.button.
"""
class WhiteRoundedButton(Button):
"""
Custom button style for the "back to menu" and "reset" buttons.
It inherits from kivy.uix.button.
"""
class QuestionLabel(Label):
"""
Custom Label that wraps its text in case it is too long.
It inherits from from kivy.uix.label
"""
class StudyInstructionsPopup(Popup):
"""
Custom PopUp to show usage instructions.
It inherits from from kivy.uix.popup
"""
class QuizInstructionsPopup(Popup):
"""
Custom PopUp to show usage instructions.
It inherits from from kivy.uix.popup
"""
class OhmInstructionsPopup(Popup):
"""
Custom PopUp to show usage instructions.
It inherits from from kivy.uix.popup
"""
class OhmCalcInstructionsPopup(Popup):
"""
Custom PopUp to show usage instructions.
It inherits from from kivy.uix.popup
"""
class KirchhoffInstructionsCarouselPopup(Popup):
"""
Custom PopUp to show usage instructions.
It inherits from from kivy.uix.popup
"""
class KirchhoffExamplesCarouselPopup(Popup):
"""
Custom PopUp to show usage instructions.
It inherits from from kivy.uix.popup
"""
class KirchhoffFormulasPopup(Popup):
"""
Custom PopUp to show formulas.
It inherits from from kivy.uix.popup
"""
class OhmMissingValuesPopup(Popup):
"""
Custom PopUp to a warning about missing required values.
It inherits from from kivy.uix.popup
"""
class KirchhoffPopup(Popup):
"""
Custom PopUp to a warning in the Kirchhoff screen.
It inherits from from kivy.uix.popup
"""
class ResetButton(PurpleRoundedButton):
"""
Custom reset button.
It inherits from PurpleRoundedButton.
"""
class InstructionsButton(PurpleRoundedButton):
"""
Custom instructions button.
It inherits from PurpleRoundedButton.
"""
class SettingsButton(MenuScreenButton):
"""
Custom Settings button style for the Menu Screen.
It inherits from MenuScreenButton, which inherits from kivy.uix.button.
"""
class AboutButton(MenuScreenButton):
"""
Custom "About" button style for the Menu Screen.
It inherits from MenuScreenButton, which inherits from kivy.uix.button.
"""
class AboutScreen(Screen):
"""
Custom Screen Class for the About section.
It inherits from Screen.
"""
class ResetQuizButton(ResetButton):
"""
Custom reset button for the Quiz screen.
It inherits from kivy.uix.button.
"""
def on_press(self):
"""Set the Sliders values to their defaults."""
# Reset the counters.
self.parent.parent.correct_questions_counter = 0
self.parent.parent.incorrect_questions_counter = 0
self.parent.parent.result_label.color = (0, 0, 0, 0)
self.parent.parent.correct_question_counter_label.font_size = "15sp"
self.parent.parent.correct_question_counter_label.color = (1, 1, 1, 1)
self.parent.parent.incorrect_question_counter_label.font_size = "15sp"
self.parent.parent.incorrect_question_counter_label.color = (1, 1, 1, 1)
# Re-draw the answers counters.
self.parent.parent.correct_question_counter_label.text = \
str(self.parent.parent.correct_questions_counter)
self.parent.parent.incorrect_question_counter_label.text = \
str(self.parent.parent.incorrect_questions_counter)
class ResetCalcButton(WhiteRoundedButton):
"""
Custom reset button for the Calculator screen.
It inherits from kivy.uix.button.
"""
def on_press(self):
"""Set the Sliders values to their defaults."""
# Reset the inputs.
inputs_list = [self.parent.parent.parent.ohm_input,
self.parent.parent.parent.amp_input,
self.parent.parent.parent.volt_input]
for input in inputs_list:
input.text = ""
class ResetOhmButton(ResetButton):
"""
Custom reset button for the Ohm screen.
It inherits from kivy.uix.button.
"""
def on_press(self):
"""Set the Sliders values to their defaults."""
# Reset to the default image.
self.parent.parent.triangle_image.source = \
"assets/images/I_318px-law_triangle.png"
self.parent.parent.selected = "current"
# Reset sliders' status.
self.parent.parent.current_slider.disabled = True
self.parent.parent.voltage_slider.disabled = False
self.parent.parent.resistance_slider.disabled = False
# Reset sliders' values.
self.parent.parent.current_slider.value = 0.2
self.parent.parent.current_slider.min = 0.1
self.parent.parent.current_slider.max = 900
self.parent.parent.voltage_slider.value = 0.1
self.parent.parent.voltage_slider.min = 0.1
self.parent.parent.voltage_slider.max = 9
self.parent.parent.resistance_slider.value = 500
self.parent.parent.resistance_slider.min = 10
self.parent.parent.resistance_slider.max = 1000
class StudyScreen(Screen):
"""
Custom Screen Class for the study section.
It inherits from Screen.
"""
def __init__(self, **kwargs):
"""Class constructor."""
super(StudyScreen, self).__init__(**kwargs)
# Create a float layout.
self.float_layout = FloatLayout()
self.add_widget(self.float_layout)
# Add the right side of the screen, where we will show the text.
self.rst_document = RstDocument(source="assets/chapters/english/chapter01.rst",
show_errors=True,
size_hint=(0.8, 1),
pos_hint={"right":1, "top":1})
self.float_layout.add_widget(self.rst_document)
# Add the dropdown "menu" at the left of the screen.
self.dropdown = DropDown()
for chapter in ['01', '02', '03']:
# when adding widgets, we need to specify the height manually
# (disabling the size_hint_y) so the dropdown can calculate the
# area it needs.
btn = PurpleRoundedButton(text=chapter,
height=400,
size_hint_y=None)
# For each button, attach a callback that will call the select()
# method on the dropdown. We'll pass the text of the button as
# the data of the selection.
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
# Add another bind so the RST document is updated according to
# the selection.
btn.bind(on_press=lambda btn: self.update_study_text(btn.text))
# Add the button inside the dropdown.
self.dropdown.add_widget(btn)
# Create a big main button to open the dropdown.
self.mainbutton = PurpleRoundedButton(text='Lessons',
size_hint=(0.2, 0.2),
pos_hint={"left": 0, "top": 1})
# Show the dropdown menu when the main button is released
# note: all the bind() calls pass the instance of the caller (here, the
# mainbutton instance) as the first argument of the callback (here,
# dropdown.open).
self.mainbutton.bind(on_release=self.dropdown.open)
# Listen for the selection in the dropdown list and
# assign the data to the button text.
self.dropdown.bind(on_select=lambda instance, x: setattr(self.mainbutton, 'text', x))
self.float_layout.add_widget(self.mainbutton)
# Add a button explaining how to use this section.
self.study_instructions_button = \
InstructionsButton(pos_hint={"center_x": 0.1, "bottom": 1})
self.study_instructions_button.bind(on_press=self.show_instructions)
self.float_layout.add_widget(self.study_instructions_button)
def show_instructions(self, instance):
"""Display instructions for this section."""
popup = StudyInstructionsPopup()
popup.open()
def update_study_text(self, chapter):
"""Update the text in the RST widget according to the dropdown selection."""
self.rst_document.source = \
"assets/chapters/english/chapter" + chapter.replace("'", "") + ".rst"
class QuizScreen(Screen):
"""
Custom Screen Class for the Quiz section.
It inherits from Screen.
"""
def __init__(self, **kwargs):
"""Class constructor."""
super(QuizScreen, self).__init__(**kwargs)
# Place holders for questions' data.
self.question_id = ""
self.question_text = ""
self.correct_answer = ""
self.correct_questions_counter = 0
self.incorrect_questions_counter = 0
# Create a float layout.
self.float_layout = FloatLayout()
self.add_widget(self.float_layout)
# Add a button explaining how to use this section.
self.quiz_instructions_button = \
InstructionsButton(pos_hint={"center_x": 0.1, "bottom": 1})
self.quiz_instructions_button.bind(on_press=self.show_instructions)
self.float_layout.add_widget(self.quiz_instructions_button)
# A label to show "Correct!" or "Incorrect!"
# depending on the selected answer.
self.result_label = Label(text="",
size_hint=(0.5, 0.05),
color=(0, 0, 0, 0),
pos_hint={"center_x": 0.5, "top": 0.95})
self.float_layout.add_widget(self.result_label)
# Labels for correct/incorrect questions counters.
# Create a grid layout for the correct/incorrect display.
self.answers_counters_grid_layout = \
GridLayout(rows=2, cols=2,
pos_hint={"top": 0.95, "right": 0.95},
size_hint=(0.2, 0.1))
correct_answer_icon = Image(source="assets/icons/ic_check_white_48dp.png",
keep_ratio=True,
allow_stretch=True)
self.answers_counters_grid_layout.add_widget(correct_answer_icon)
# Add the correct question counter label to the grid layout.
self.correct_question_counter_label = \
Label(text=str(self.correct_questions_counter), font_size="20sp")
self.answers_counters_grid_layout.add_widget(
self.correct_question_counter_label)
incorrect_answer_icon = Image(source="assets/icons/ic_close_white_48dp.png",
keep_ratio=True,
allow_stretch=True)
self.answers_counters_grid_layout.add_widget(incorrect_answer_icon)
# Add the incorrect question counter label to the grid layout.
self.incorrect_question_counter_label = \
Label(text=str(self.incorrect_questions_counter),
font_size="20sp")
self.answers_counters_grid_layout.add_widget(
self.incorrect_question_counter_label)
# Add the grid layout to the float layout.
self.float_layout.add_widget(self.answers_counters_grid_layout)
# Question label
self.question_label = QuestionLabel(halign="center",
valign="middle",
font_size="15sp",
pos_hint={"center_x": 0.5,
"top": 0.9})
self.float_layout.add_widget(self.question_label)
# Create a box layout inside the float layout (to contain the answer
# buttons).
self.answers_box_layout = BoxLayout(orientation="vertical",
spacing=20,
pos_hint={"center_x": 0.5,
"top": 0.7},
size_hint=(0.5, 0.6))
self.float_layout.add_widget(self.answers_box_layout)
# Outside the grid layout (but inside the float layout), add the
# reset button.
reset_quiz_button = ResetQuizButton(pos_hint={"right": 1, "bottom": 1},
size_hint=(0.2, 0.1))
self.float_layout.add_widget(reset_quiz_button)
# Connect to the SQLite DB and create a cursor.
project_dir = os.path.dirname(os.path.realpath(__file__))
connection = sqlite3.connect(os.path.join(os.sep, project_dir,
"db", "questions.db"))
self.cursor = connection.cursor()
# No questions have been retrieved yet.
self.last_question_id = ""
# Get a random question from the DB and display it.
self.get_random_question()
def show_instructions(self, instance):
"""Display instructions for this section."""
popup = QuizInstructionsPopup()
popup.open()
def get_random_question(self):
"""Retrieve data from the SQLite DB."""
# Get a random question, but not the previous question!
self.cursor.execute(
"SELECT * FROM questions "
f"WHERE question_id NOT IN ('{self.last_question_id}') "
"AND language = 'english'"
"ORDER BY RANDOM() LIMIT 1")
# "Fetch all" returns a list with a tuple.
question = self.cursor.fetchall()
self.last_question_id = str(question[0][0])
# Show the question text into the question label.
self.question_label.text = str(question[0][1])
# Get answers to this question, and display them.
self.get_answers()
def get_answers(self):
"""Get the corresponding answers for the current question."""
# Execute the query to get the answers to the current question.
self.cursor.execute(
"SELECT * FROM answers "
"INNER JOIN questions "
"ON questions.question_id = answers.question_id "
f"WHERE questions.question_text = '{self.question_label.text}'")
# "Fetch all" returns a list with a tuple.
answers = self.cursor.fetchall()
# Get the correct answer.
self.cursor.execute(
"SELECT answer_text FROM answers "
"INNER JOIN questions "
"ON questions.question_id = answers.question_id "
f"WHERE questions.question_text = '{self.question_label.text}'"
"AND answers.is_correct = 1")
self.correct_answer = self.cursor.fetchall()[0][0]
# Format that into a list.
answers_list = [answer[1] for answer in answers]
# Shuffle the answers to make the quiz harder.
random.shuffle(answers_list)
# Create the answer buttons.
for answer in answers_list:
answer_button = WhiteRoundedButton(text=answer)
# For each button, attach a callback that will call the
# check_answer method.
# We'll pass the text of the button as the data of the selection.
answer_button.bind(on_release=lambda answer_button:
self.check_answer(answer_button.text))
# Add the button inside the box layout for the answers.
self.answers_box_layout.add_widget(answer_button)
def animate_scoreboard(self, object_to_animate):
animation = Animation(color=(1, 1, 0, 1.0),
duration=0.1) & \
Animation(font_size=object_to_animate.font_size+10,
duration=0.1) + \
Animation(color=object_to_animate.color,
duration=0.1) + \
Animation(font_size=object_to_animate.font_size,
duration=0.1)
# Start the animation.
animation.start(object_to_animate)
def animate_result_notification(self, result):
# Change the result notification text accordingly.
self.result_label.text = result
if result == "Wrong":
# Change text color to red.
animation = Animation(color=(1, 0, 0, 1),
duration=0.1) & \
Animation(font_size=self.result_label.font_size+20,
duration=0.1) + \
Animation(color=self.result_label.color,
duration=0.5) + \
Animation(font_size=self.result_label.font_size,
duration=0.5)
elif result == "Right!":
# Change text color to green.
animation = Animation(color=(0, 1, 0, 1), duration=0.1) & \
Animation(font_size=self.result_label.font_size+20,
duration=0.1) + \
Animation(color=self.result_label.color,
duration=0.5) + \
Animation(font_size=self.result_label.font_size,
duration=0.5)
animation.start(self.result_label)
def check_answer(self, selected_answer):
"""Check if the selected answer is correct or not."""
# "Reset" all the previous animations changes that may be already
# running.
self.result_label.font_size = "15sp"
self.result_label.color = (0, 0, 0, 0)
self.correct_question_counter_label.font_size = "20sp"
self.correct_question_counter_label.color = (1, 1, 1, 1)
self.incorrect_question_counter_label.font_size = "20sp"
self.incorrect_question_counter_label.color = (1, 1, 1, 1)
# Check if the selected answer is correct or not.
if selected_answer == self.correct_answer:
# Increment the correct answer counter.
self.correct_questions_counter += 1
# Start animations.
self.animate_scoreboard(self.correct_question_counter_label)
self.animate_result_notification("Right!")
else:
# Increment the incorrect answer counter.
self.incorrect_questions_counter += 1
# Start animations.
self.animate_scoreboard(self.incorrect_question_counter_label)
self.animate_result_notification("Wrong")
# Re-draw the answers counters.
self.correct_question_counter_label.text = \
str(self.correct_questions_counter)
self.incorrect_question_counter_label.text = \
str(self.incorrect_questions_counter)
# Destroy the answer buttons (to prepare for the new ones).
self.answers_box_layout.clear_widgets()
# Call the get_random_question method again.
self.get_random_question()
class KirchhoffScreen(Screen):
"""
Custom Screen Class for Kirchhoff study.
It inherits from Screen.
"""
def __init__(self, **kwargs):
"""Class constructor."""
super(KirchhoffScreen, self).__init__(**kwargs)
# Create a float layout.
self.float_layout = FloatLayout()
self.add_widget(self.float_layout)
# Add the circuit image.
self.circuit_image = Image(source="assets/images/circuit_tall.png",
keep_ratio=False,
size_hint=(0.6, 0.55),
allow_stretch=True,
pos_hint={"center_x": 0.5, "top": 0.8})
self.float_layout.add_widget(self.circuit_image)
# Add a box layout for the V1 Label+input combo (to make it easier
# to move it all together).
self.V1_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.3, 0.05),
pos_hint={"right": 0.4, "top": 0.8})
# Voltage 1 label.
self.V1_label = Label(text="V1=",
size_hint_x=0.5,
pos_hint={'right': 1})
self.V1_box_layout.add_widget(self.V1_label)
# Add the grid layout to the general float layout.
self.float_layout.add_widget(self.V1_box_layout)
# Voltage 1 input box.
self.V1_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="V1")
self.V1_box_layout.add_widget(self.V1_input)
# Voltage 1 units label.
self.V1_units_label = Label(text="V",
size_hint_x=0.15,
pos_hint={'left': 1})
self.V1_box_layout.add_widget(self.V1_units_label)
# Add a box layout for the V2 Label+input combo (to make it easier
# to move it all together).
self.V2_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.2, 0.05),
pos_hint={"right": 0.55, "top": 0.6})
# Voltage 2 label.
self.V2_label = Label(text="V2=",
size_hint_x=0.5,
pos_hint={'right': 1})
self.V2_box_layout.add_widget(self.V2_label)
# Add the grid layout to the general float layout.
self.float_layout.add_widget(self.V2_box_layout)
# Voltage 2 input box.
self.V2_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="V2")
self.V2_input.bind(text=self.v2v3_equality)
self.V2_box_layout.add_widget(self.V2_input)
# Voltage 2 units label.
self.V2_units_label = Label(text="V",
size_hint_x=0.15,
pos_hint={'left': 1})
self.V2_box_layout.add_widget(self.V2_units_label)
# Add a box layout for the V3 Label+input combo (to make it easier
# to move it all together).
self.V3_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.2, 0.05),
pos_hint={"right": 0.98, "top": 0.6})
self.float_layout.add_widget(self.V3_box_layout)
# Voltage 3 label.
self.V3_label = Label(text="V3=",
size_hint_x=0.5,
pos_hint={'right': 1},
markup=True)
self.V3_box_layout.add_widget(self.V3_label)
# Voltage 3 input box.
self.V3_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="V3")
self.V3_input.bind(text=self.v2v3_equality)
self.V3_box_layout.add_widget(self.V3_input)
# Voltage 3 units label.
self.V3_units_label = Label(text="V",
size_hint_x=0.15,
pos_hint={'left': 1})
self.V3_box_layout.add_widget(self.V3_units_label)
# Add a box layout for the V4 Label+input combo (to make it easier
# to move it all together).
self.V4_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.3, 0.05),
pos_hint={"right": 0.4, "top": 0.3})
self.float_layout.add_widget(self.V4_box_layout)
# Voltage 4 label.
self.V4_label = Label(text="V4=",
size_hint_x=0.5,
pos_hint={'right': 1},
markup=True)
self.V4_box_layout.add_widget(self.V4_label)
# Voltage 4 input box.
self.V4_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="V4",
disabled=True)
self.V4_box_layout.add_widget(self.V4_input)
# Voltage 4 units label.
self.V4_units_label = Label(text="V",
size_hint_x=0.15,
pos_hint={'left': 1})
self.V4_box_layout.add_widget(self.V4_units_label)
# Add a box layout for the I1 Label+input combo (to make it easier
# to move it all together).
self.I1_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.3, 0.05),
pos_hint={"right": 0.8, "top": 0.8})
self.float_layout.add_widget(self.I1_box_layout)
# Current 1 label.
self.I1_label = Label(text="I1=",
size_hint_x=0.2,
pos_hint={'right': 1})
self.I1_box_layout.add_widget(self.I1_label)
# Current 1 input box.
self.I1_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="I1")
self.I1_box_layout.add_widget(self.I1_input)
# Current 1 units label.
self.I1_units_label = Label(text="A",
size_hint_x=0.15,
pos_hint={'left': 1})
self.I1_box_layout.add_widget(self.I1_units_label)
# Add a box layout for the I2 Label+input combo (to make it easier
# to move it all together).
self.I2_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.2, 0.05),
pos_hint={"right": 0.55, "top": 0.5})
self.float_layout.add_widget(self.I2_box_layout)
# Resistance 2 label.
self.I2_label = Label(text="I2=",
size_hint_x=0.25,
pos_hint={'right': 1})
self.I2_box_layout.add_widget(self.I2_label)
# Resistance 2 input box.
self.I2_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="I2")
self.I2_box_layout.add_widget(self.I2_input)
# Current 2 units label.
self.I2_units_label = Label(text="A",
size_hint_x=0.15,
pos_hint={'left': 1})
self.I2_box_layout.add_widget(self.I2_units_label)
# Add a box layout for the I3 Label+input combo (to make it easier
# to move it all together).
self.I3_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.2, 0.05),
pos_hint={"right": 0.98, "top": 0.5})
self.float_layout.add_widget(self.I3_box_layout)
# Resistance 3 label.
self.I3_label = Label(text="I3=",
size_hint_x=0.25,
pos_hint={'right': 1})
self.I3_box_layout.add_widget(self.I3_label)
# Resistance 3 input box.
self.I3_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="I3")
self.I3_box_layout.add_widget(self.I3_input)
# Current 3 units label.
self.I3_units_label = Label(text="A",
size_hint_x=0.15,
pos_hint={'left': 1})
self.I3_box_layout.add_widget(self.I3_units_label)
# Add a box layout for the I4 Label+input combo (to make it easier
# to move it all together).
self.I4_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.3, 0.05),
pos_hint={"right": 0.8, "top": 0.3})
self.float_layout.add_widget(self.I4_box_layout)
# Current 4 label.
self.I4_label = Label(text="I4=",
size_hint_x=0.25,
pos_hint={'right': 1})
self.I4_box_layout.add_widget(self.I4_label)
# Current 4 input box.
self.I4_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
disabled=True,
id="I4")
self.I4_box_layout.add_widget(self.I4_input)
# Current 4 units label.
self.I4_units_label = Label(text="A",
size_hint_x=0.15,
pos_hint={'left': 1})
self.I4_box_layout.add_widget(self.I4_units_label)
# Add a box layout for the VT Label+input combo (to make it easier
# to move it all together).
self.VT_box_layout = BoxLayout(orientation="horizontal",
spacing=5,
size_hint=(0.2, 0.05),
pos_hint={"right": 0.23, "top": 0.55})
self.float_layout.add_widget(self.VT_box_layout)
# VT label.
self.VT_label = Label(text="VT=",
size_hint_x=0.5,
pos_hint={'right': 1})
self.VT_box_layout.add_widget(self.VT_label)
# VT input box.
self.VT_input = TextInput(font_size="18sp",
input_type="number",
multiline=False,
input_filter="float",
halign='center',
id="VT")
self.VT_box_layout.add_widget(self.VT_input)
# Voltage T units label.
self.VT_units_label = Label(text="V",
size_hint_x=0.15,
pos_hint={'left': 1})
self.VT_box_layout.add_widget(self.VT_units_label)
# Add a selection between the two Kirchhoff laws.
self.laws_selection_layout = GridLayout(cols=2,
rows=2,
spacing=0,
pos_hint={"center_x": 0.5,
"top": 0.95},
size_hint=(0.9, 0.08))
self.float_layout.add_widget(self.laws_selection_layout)
# Add a radio button group with the 2 options.
self.kirchhoff_current_checkbox = CheckBox()
self.kirchhoff_current_checkbox.group = "option_kirchhoff"
self.kirchhoff_current_checkbox.id = "current_checkbox"
self.laws_selection_layout.add_widget(self.kirchhoff_current_checkbox)
# Attach a callback.
self.kirchhoff_current_checkbox.bind(active=self.on_checkbox_Active)
self.kirchhoff_voltage_checkbox = CheckBox()
self.kirchhoff_voltage_checkbox.group = "option_kirchhoff"
self.kirchhoff_voltage_checkbox.id = "voltage_checkbox"
self.laws_selection_layout.add_widget(self.kirchhoff_voltage_checkbox)
# Attach a callback.
self.kirchhoff_voltage_checkbox.bind(active=self.on_checkbox_Active)
# Add a label for each radio button.
current_label = Label(text="Current",
halign='center',
font_size="15sp")
self.laws_selection_layout.add_widget(current_label)
voltage_label = Label(text="Voltage",
halign='center',
font_size="15sp")
self.laws_selection_layout.add_widget(voltage_label)
# Add another grid layout for the buttons.
self.buttons_grid_layout = GridLayout(cols=5,
rows=1,
spacing=10,
pos_hint={"center_x": 0.5,
"bottom": 1},
size_hint=(0.9, 0.1))
self.float_layout.add_widget(self.buttons_grid_layout)
# Add the instructions button.
self.instructions_button = PurpleRoundedButton(text="Guide",
font_size="13sp")
self.instructions_button.bind(on_press=self.show_instructions_carousel)
self.buttons_grid_layout.add_widget(self.instructions_button)
# Add the reset button.
self.reset_button = ResetButton(text="Reset",
font_size="13sp")
self.reset_button.bind(on_press=self.reset_kirchhoff_values)
self.buttons_grid_layout.add_widget(self.reset_button)
# Add the formulas button.
self.formulas_button = PurpleRoundedButton(text="Formulas",
font_size="13sp")
self.formulas_button.bind(on_press=self.show_formulas)
self.buttons_grid_layout.add_widget(self.formulas_button)
# Add a "Example" button.
self.example_button = PurpleRoundedButton(text="Example",
font_size="13sp")
self.example_button.bind(on_press=self.show_example_carousel)
self.buttons_grid_layout.add_widget(self.example_button)
# Add a "Calculate" button.
self.calculate_button = PurpleRoundedButton(text="Calculate",
font_size="13sp")
self.calculate_button.bind(on_press=self.validate_inputs)
self.buttons_grid_layout.add_widget(self.calculate_button)
# Once all the items are in place, activate one of the
# radio buttons by default.
self.kirchhoff_current_checkbox.state = "down"
def show_example_carousel(self, instance):
"""Show the example carousel popup."""
popup = KirchhoffExamplesCarouselPopup()
popup.open()
def show_instructions_carousel(self, instance):
"""Display instructions for this section."""
popup = KirchhoffInstructionsCarouselPopup()
popup.open()
def show_formulas(self, instance):
"""Display formulas for this section."""
popup = KirchhoffFormulasPopup()
popup.open()
# Callback for the checkbox
def on_checkbox_Active(self, checkboxInstance, isActive):
"""Deactivate the other two input fields and reset values."""
if checkboxInstance.id == "voltage_checkbox":
self.I1_input.disabled = True
self.I2_input.disabled = True
self.I3_input.disabled = True
self.VT_input.disabled = False
self.V1_input.disabled = False
self.V2_input.disabled = False
self.V3_input.disabled = False
elif checkboxInstance.id == "current_checkbox":
self.I1_input.disabled = False
self.I2_input.disabled = False
self.I3_input.disabled = False
self.VT_input.disabled = True
self.V1_input.disabled = True
self.V2_input.disabled = True
self.V3_input.disabled = True
def v2v3_equality(self, instance, value):
"""On the key press, make sure V2 and V3 have the same text."""
if instance.id == "V3":
self.V2_input.text = self.V3_input.text
else:
self.V3_input.text = self.V2_input.text
def validate_inputs(self, instance):
"""Validate appropiate inputs (before calculating results)."""
# Check if the users wants to calculate Voltage or Current.
if self.kirchhoff_voltage_checkbox.active:
fields_to_check = [self.VT_input,
self.V1_input,
self.V2_input,
self.V3_input]
else:
fields_to_check = [self.I1_input]
# A list to contain the IDs of the fields which are missing values.
empty_fields = [field.id for field in fields_to_check if "" == field.text]
# A list to contain the IDs of the fields with negative values.
negative_fields = [field.id for field in fields_to_check if "-" in field.text]
if empty_fields:
message = f"Please enter a value in {', '.join(empty_fields)}"
popup = KirchhoffPopup()
popup.title = "Oops! One or more values are missing!"
popup.label_text = message
popup.open()
elif negative_fields:
message = f"Please enter a positive value in {', '.join(negative_fields)}"
popup = KirchhoffPopup()
popup.title = "Negative values are not allowed!"
popup.label_text = message
popup.open()
elif self.kirchhoff_voltage_checkbox.active and \