Advertisement
shh_algo_PY

main.py for Kivy Lesson 4

Mar 18th, 2023
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.49 KB | None | 0 0
  1. # write your app here
  2.  
  3. from kivy.app import App
  4. from kivy.uix.screenmanager import ScreenManager, Screen
  5. from kivy.uix.boxlayout import BoxLayout
  6. from kivy.uix.label import Label
  7. from kivy.uix.button import Button
  8. from kivy.uix.textinput import TextInput
  9. from kivy.core.window import Window
  10. from kivy.uix.scrollview import ScrollView
  11. from instructions import txt_instruction, txt_test1, txt_test2, txt_test3, txt_sits
  12. from ruffier import test
  13.  
  14. from seconds import Seconds
  15. from sits import Sits
  16. from runner import Runner
  17.  
  18. Window.clearcolor = (.87, 0.54, 0.8, 0.3)
  19. btn_color = (0.98, 0.31, 0.8, 1)
  20.  
  21. age = 7
  22. name = ""
  23. p1, p2, p3 = 0, 0, 0
  24.  
  25. def check_int(str_num):
  26.     try:
  27.         return int(str_num)
  28.     except:
  29.         return False
  30.  
  31. class InstrScr(Screen):
  32.     def __init__(self, **kwargs):
  33.         super().__init__(**kwargs)
  34.         instr = Label(text=txt_instruction)
  35.         lbl1 = Label(text='Enter your name:', halign='right')
  36.         self.in_name = TextInput(multiline=False)
  37.         lbl2 = Label(text='Enter your age:', halign='right')
  38.         self.in_age = TextInput(text='7', multiline=False)
  39.         self.btn = Button(text='Start', size_hint=(0.3, 0.2), pos_hint={'center_x': 0.5})
  40.         self.btn.background_color = btn_color
  41.         self.btn.on_press = self.next
  42.         line1 = BoxLayout(size_hint=(0.8, None), height='30sp')
  43.         line2 = BoxLayout(size_hint=(0.8, None), height='30sp')
  44.         line1.add_widget(lbl1)
  45.         line1.add_widget(self.in_name)
  46.         line2.add_widget(lbl2)
  47.         line2.add_widget(self.in_age)
  48.         outer = BoxLayout(orientation='vertical', padding=8, spacing=8)
  49.         outer.add_widget(instr)
  50.         outer.add_widget(line1)
  51.         outer.add_widget(line2)
  52.         outer.add_widget(self.btn)
  53.         self.add_widget(outer)
  54.     def next(self):
  55.         name = self.in_name.text
  56.         age = check_int(self.in_age.text)
  57.         if age == False or age < 7:
  58.             age = 7
  59.             self.in_age.text = str(age)
  60.         else:
  61.             self.manager.current = 'pulse1'
  62.  
  63. class PulseScr(Screen):
  64.     def __init__(self, **kwargs):
  65.         super().__init__(**kwargs)
  66.         self.next_screen = False
  67.        
  68.         instr = Label(text=txt_test1)
  69.         self.lbl_sec = Seconds(15)
  70.         self.lbl_sec.bind(done=self.sec_finished)
  71.  
  72.  
  73.         line = BoxLayout(size_hint=(0.8, None), height='30sp')
  74.         lbl_result = Label(text='Enter the result:', halign='right')
  75.         self.in_result = TextInput(text='0', multiline=False)
  76.         self.in_result.set_disabled(True)
  77.        
  78.         line.add_widget(lbl_result)
  79.         line.add_widget(self.in_result)
  80.         self.btn = Button(text='Start', size_hint=(0.3, 0.4), pos_hint={'center_x': 0.5})
  81.         self.btn.background_color = btn_color
  82.         self.btn.on_press = self.next
  83.         outer = BoxLayout(orientation='vertical', padding=8, spacing=8)
  84.         outer.add_widget(instr)
  85.         #outer.add_widget(lbl1)
  86.         outer.add_widget(self.lbl_sec)
  87.         outer.add_widget(line)
  88.         outer.add_widget(self.btn)
  89.         self.add_widget(outer)
  90.  
  91.     def sec_finished(self, *args):
  92.         self.next_screen = True
  93.         self.in_result.set_disabled(False)
  94.         self.btn.set_disabled(False)
  95.         self.btn.text = 'Continue'
  96.  
  97.     def next(self):
  98.         if not self.next_screen:
  99.             self.btn.set_disabled(True)
  100.             self.lbl_sec.start()
  101.         else:
  102.             global p1
  103.             p1 = check_int(self.in_result.text)
  104.             if p1 == False or p1 <= 0:
  105.                 p1 = 0
  106.                 self.in_result.text = str(p1)
  107.             else:
  108.                 self.manager.current = 'sits'
  109.  
  110. class CheckSits(Screen):
  111.     def __init__(self, **kwargs):
  112.         super().__init__(**kwargs)
  113.         self.next_screen = False
  114.  
  115.         instr = Label(text=txt_sits, size_hint=(0.5, 1))
  116.         self.lbl_sits = Sits(30)
  117.         self.run = Runner(total=30, steptime=1.5, size_hint=(0.4, 1))
  118.         self.run.bind(finished=self.run_finished)
  119.  
  120.         line = BoxLayout()
  121.         vlay = BoxLayout(orientation='vertical', size_hint=(0.3, 1))
  122.         vlay.add_widget(self.lbl_sits)
  123.         line.add_widget(instr)
  124.         line.add_widget(vlay)
  125.         line.add_widget(self.run)
  126.  
  127.         self.btn = Button(text='Start', size_hint=(0.3, 0.2), pos_hint={'center_x': 0.5})
  128.         self.btn.background_color = btn_color
  129.         self.btn.on_press = self.next
  130.  
  131.         outer = BoxLayout(orientation='vertical', padding=8, spacing=8)
  132.         outer.add_widget(line)
  133.         outer.add_widget(self.btn)
  134.  
  135.         self.add_widget(outer)
  136.  
  137.     def run_finished(self, instance, value):
  138.         self.btn.set_disabled(False)
  139.         self.btn.text = 'Continue'
  140.         self.next_screen = True
  141.  
  142.     def next(self):
  143.         if not self.next_screen:
  144.             self.btn.set_disabled(True)
  145.             self.run.start()
  146.             self.run.bind(value=self.lbl_sits.next)
  147.         else:
  148.             self.manager.current = 'pulse2'
  149.  
  150. class PulseScr2(Screen):
  151.     def __init__(self, **kwargs):
  152.         self.next_screen = False
  153.  
  154.         self.stage = 0
  155.         super().__init__(**kwargs)
  156.         instr = Label(text=txt_test3)
  157.         line1 = BoxLayout(size_hint=(0.8, None), height='30sp')
  158.         self.lbl_sec = Seconds(15)
  159.         self.lbl_sec.bind(done=self.sec_finished)
  160.         self.lbl1 = Label(text='Calculate your heart rate')
  161.  
  162.         lbl_result1 = Label(text='Result:', halign='right')
  163.         self.in_result1 = TextInput(text='0', multiline=False)
  164.         line1.add_widget(lbl_result1)
  165.         line1.add_widget(self.in_result1)
  166.         line2 = BoxLayout(size_hint=(0.8, None), height='30sp')
  167.         lbl_result2 = Label(text='Result after rest:', halign='right')
  168.         self.in_result2 = TextInput(text='0', multiline=False)
  169.  
  170.         self.in_result1.set_disabled(True)
  171.         self.in_result2.set_disabled(True)
  172.         line2.add_widget(lbl_result2)
  173.         line2.add_widget(self.in_result2)
  174.         self.btn = Button(text='Start', size_hint=(0.3, 0.5), pos_hint={'center_x': 0.5})
  175.         self.btn.background_color = btn_color
  176.         self.btn.on_press = self.next
  177.         outer = BoxLayout(orientation='vertical', padding=8, spacing=8)
  178.         outer.add_widget(instr)
  179.         outer.add_widget(self.lbl1)
  180.         outer.add_widget(self.lbl_sec)
  181.         outer.add_widget(line1)
  182.         outer.add_widget(line2)
  183.         outer.add_widget(self.btn)
  184.         self.add_widget(outer)
  185.  
  186.     def sec_finished(self, *args):
  187.         if self.lbl_sec.done == True:
  188.             if self.stage == 0:
  189.                 # finished the first count, resting
  190.                 self.stage = 1
  191.                 self.lbl1.text = 'Rest'
  192.                 self.lbl_sec.restart(30)
  193.                 self.in_result1.set_disabled(False)
  194.             elif self.stage == 1:
  195.                 # finished rest, calculating
  196.                 self.stage = 2
  197.                 self.lbl1.text='Calculate your heart rate'
  198.                 self.lbl_sec.restart(15)
  199.             elif self.stage == 2:
  200.                 self.in_result2.set_disabled(False)
  201.                 self.btn.set_disabled(False)
  202.                 self.btn.text = 'End'
  203.                 self.next_screen = True
  204.  
  205.     def next(self):
  206.         if not self.next_screen:
  207.             self.btn.set_disabled(True)
  208.             self.lbl_sec.start()
  209.         else:
  210.             global p2, p3
  211.             p2 = check_int(self.in_result1.text)
  212.             p3 = check_int(self.in_result2.text)
  213.             if p2 == False:
  214.                 p2 = 0
  215.                 self.in_result1.text = str(p2)
  216.             elif p3 == False:
  217.                 p3 = 0
  218.                 self.in_result2.text = str(p3)
  219.             else:
  220.                 # transitioning
  221.                 self.manager.current = 'result'
  222.  
  223. class Result(Screen):
  224.     def __init__(self, **kwargs):
  225.         super().__init__(**kwargs)
  226.         self.outer = BoxLayout(orientation='vertical', padding=8, spacing=8)
  227.         self.instr = Label(text = '')
  228.         self.outer.add_widget(self.instr)
  229.         self.add_widget(self.outer)
  230.         self.on_enter = self.before
  231.  
  232.     def before(self):
  233.         global name
  234.         self.instr.text = name + '\n' + test(p1, p2, p3, age)
  235.  
  236. class HeartCheck(App):
  237.     def build(self):
  238.         sm = ScreenManager()
  239.         sm.add_widget(InstrScr(name='instr'))
  240.         sm.add_widget(PulseScr(name='pulse1'))
  241.         sm.add_widget(CheckSits(name='sits'))
  242.         sm.add_widget(PulseScr2(name='pulse2'))
  243.         sm.add_widget(Result(name='result'))
  244.         return sm
  245.  
  246. app = HeartCheck()
  247. app.run()
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement