Advertisement
makut

Dotan

Nov 26th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.38 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.uix.button import Button
  3. from kivy.uix.label import Label
  4. from kivy.uix.floatlayout import FloatLayout
  5. from kivy.clock import Clock
  6. from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
  7. from kivy.properties import ObjectProperty
  8. from kivy.uix.textinput import TextInput
  9. import time
  10. import os
  11.  
  12. class RegistrationScreen(App):
  13.     def build(self):
  14.         self.layout = FloatLayout(size=(100, 200))
  15.         self.registration_text = Label(text='Registration', font_size=font_size_coeff * 60, pos_hint={'x': 0.01, 'y': 0.2})
  16.         self.input_name = TextInput(text='Enter your name', focus=False,
  17.                                     multiline=False, size_hint=(font_size_coeff * 0.5, font_size_coeff * 0.1),
  18.                                     pos_hint={'x': 0.4, 'y': 0.6})
  19.         self.input_time = TextInput(text='How many hours a day do you play Dota?', multiline=False, focus=False,
  20.                                     size_hint=(font_size_coeff * 0.8, font_size_coeff * 0.1), pos_hint={'x': 0.4, 'y': 0.4})
  21.         self.but = Button(text='Enter', font_size=font_size_coeff * 40, size_hint=(0.2, 0.1))
  22.         self.but.bind(on_press=self.callback)
  23.         self.layout.add_widget(self.registration_text)
  24.         self.layout.add_widget(self.input_name)
  25.         self.layout.add_widget(self.input_time)
  26.         self.layout.add_widget(self.but)
  27.         return self.layout
  28.    
  29.     def callback(self, *args):
  30.         try:
  31.             int(self.input_time.text)
  32.             global script_path
  33.             with open(os.path.join(script_path, "data.txt"), "w") as f:
  34.                 f.write(str(self.input_name.text) + '\n' + str(int(time.time())) + '\n')
  35.                 f.write(str(self.input_time.text) + '\n')
  36.             self.stop()
  37.         except:
  38.             self.input_time.text = "You can play only integer hours a day"
  39.  
  40.  
  41. def registration():
  42.     if __name__ in ('__main__', '__android__'):
  43.         RegistrationScreen().run()
  44.  
  45. if __name__ == '__main__':
  46.     font_size_coeff = 0.5
  47. else:
  48.     fon_size_coeff = 1
  49.  
  50. script_path = os.path.dirname(os.path.realpath(__file__))
  51. try:
  52.     with open(os.path.join(script_path,"data.txt"), "r") as f:
  53.         f.readline()
  54.         START = int(f.readline().strip())
  55. except:
  56.     registration()
  57.     with open(os.path.join(script_path,"data.txt"), "r") as f:
  58.         f.readline()
  59.         START = int(f.readline().strip())
  60.  
  61. def current_time(seconds):
  62.     return str(seconds // 60) + ' minutes ' + str(seconds % 60) + ' seconds'
  63.  
  64. def main():
  65.     app = Main()
  66.     return app
  67.  
  68. def callback(instance, value):
  69.     pass
  70.  
  71. string = "You haven't played Dota for:\n" + current_time(0)
  72. height = 550
  73. width = 350
  74.  
  75.  
  76. class TimeLabel(Label):
  77.    
  78.     def update(self, *args):
  79.         self.font_size = 40
  80.         self.text = "You haven't played Dota for:\n" + current_time(int(time.time()) - START)
  81.        
  82. class Menu(Screen):
  83.    
  84.     def __init__(self, **kwargs):
  85.         super (Menu,self).__init__(**kwargs)
  86.        
  87.         global string
  88.         global height
  89.         global width
  90.         self.layout = FloatLayout(size = (width, height))
  91.         self.but1 = Button(text='Motivation', font_size=font_size_coeff * 40, size_hint = (.45, .15),
  92.                             pos_hint = {'x' : 0.02857142857142857, 'y' : 0.02857142857142857})
  93.         self.but2 = Button(text='Achievements', font_size=font_size_coeff * 40, size_hint = (.45, .15),
  94.                             pos_hint = {'x' : 0.5142857142857142, 'y' : 0.02857142857142857})
  95.         self.but3 = Button(text='Stats', font_size=font_size_coeff * 40, size_hint = (.45, .15), pos_hint = {'x' : 0.02857142857142857,
  96.                                                                                             'y' : 0.2})
  97.         self.but4 = Button(text='Advice', font_size=font_size_coeff * 40, size_hint = (.45, .15), pos_hint = {'x' : 0.5142857142857142,
  98.                                                                                             'y' : 0.2})
  99.         self.text = TimeLabel()
  100.         self.but1.bind(on_press=self.changer1)
  101.         self.but2.bind(on_press=self.changer2)
  102.         self.but3.bind(on_press=self.changer3)
  103.         self.but4.bind(on_press=self.changer4)
  104.         self.layout.add_widget(self.but1)
  105.         self.layout.add_widget(self.but2)
  106.         self.layout.add_widget(self.but3)
  107.         self.layout.add_widget(self.but4)
  108.         self.layout.add_widget(self.text)
  109.         self.text.font_size = 60
  110.         Clock.schedule_interval(self.text.update, 1)
  111.         self.add_widget(self.layout)
  112.            
  113.     def my_callback(self, dt):
  114.         self.text = "You haven't played Dota for:\n" + current_time(int(time.time() - START))        
  115.        
  116.  
  117.     def changer1(self,*args):
  118.         self.manager.transition.direction = 'left'
  119.         self.manager.current = 'motivation'
  120.            
  121.     def changer2(self,*args):
  122.         self.manager.transition.direction = 'left'
  123.         self.manager.current = 'achievements'    
  124.            
  125.     def changer3(self,*args):
  126.         self.manager.transition.direction = 'left'
  127.         self.manager.current = 'stats'
  128.            
  129.     def changer4(self,*args):
  130.         self.manager.transition.direction = 'left'
  131.         self.manager.current = 'advice'  
  132.  
  133. class Motivation(Screen):
  134.    
  135.     def __init__(self, **kwargs):
  136.         super (Motivation,self).__init__(**kwargs)
  137.        
  138.         global height
  139.         global width
  140.         self.layout = FloatLayout()
  141.         self.but1 = Button(text = 'Back to Menu ', font_size = font_size_coeff * 40, size_hint = (.2, .1), pos_hint = {'x' : 0,
  142.                                                                                                      'y' : 0.9})
  143.         self.but1.bind(on_press=self.changer)
  144.         self.layout.add_widget(self.but1)
  145.         self.add_widget(self.layout)
  146.    
  147.     def changer(self,*args):
  148.         self.manager.transition.direction = 'right'
  149.         self.manager.current = 'menu'
  150.    
  151. class Achievements(Screen):
  152.    
  153.     def __init__(self, **kwargs):
  154.         super (Achievements,self).__init__(**kwargs)
  155.        
  156.         global height
  157.         global width
  158.         self.layout = FloatLayout()
  159.         self.but1 = Button(text = 'Back to Menu ', font_size = font_size_coeff * 40, size_hint = (.2, .1), pos_hint = {'x' : 0,
  160.                                                                                      'y' : 0.9})
  161.         self.but1.bind(on_press=self.changer)
  162.         self.layout.add_widget(self.but1)
  163.         self.add_widget(self.layout)
  164.    
  165.     def changer(self,*args):
  166.         self.manager.transition.direction = 'right'
  167.         self.manager.current = 'menu'
  168.            
  169. class Stats(Screen):
  170.    
  171.     def __init__(self, **kwargs):
  172.         super (Stats,self).__init__(**kwargs)
  173.        
  174.         global height
  175.         global width
  176.         self.layout = FloatLayout()
  177.         self.but1 = Button(text = 'Back to Menu ', font_size = font_size_coeff * 40, size_hint = (.2, .1), pos_hint = {'x' : 0,
  178.                                                                                      'y' : 0.9})
  179.         self.label_time = Label(text="1) You haven't played for\n" + str(current_time(int(time.time()) - int(START))),
  180.                                 pos_hint={'x': 0.05, 'y': 0.3})
  181.         self.but1.bind(on_press=self.changer)
  182.         self.layout.add_widget(self.but1)
  183.         self.layout.add_widget(self.label_time)
  184.         self.add_widget(self.layout)
  185.    
  186.     def changer(self,*args):
  187.         self.label_time.text = "1) You haven't played for\n" + str(current_time(int(time.time()) - int(START)))
  188.         self.manager.transition.direction = 'right'
  189.         self.manager.current = 'menu'
  190.            
  191. class Advice(Screen):
  192.    
  193.     def __init__(self, **kwargs):
  194.         super (Advice,self).__init__(**kwargs)
  195.        
  196.         global height
  197.         global width
  198.         self.layout = FloatLayout()
  199.         self.but1 = Button(text = 'Back to Menu ', font_size = font_size_coeff * 40, size_hint = (.2, .1), pos_hint = {'x' : 0,
  200.                                                                                      'y' : 0.9})
  201.         self.but1.bind(on_press=self.changer)
  202.         self.layout.add_widget(self.but1)
  203.         self.add_widget(self.layout)
  204.    
  205.     def changer(self,*args):
  206.         self.manager.transition.direction = 'right'
  207.         self.manager.current = 'menu'
  208.  
  209.  
  210.  
  211.  
  212. class Main(App):
  213.    
  214.     def build(self):
  215.         global screens
  216.         self.screenmanager = ScreenManager(transition=SlideTransition())
  217.         self.menu = Menu(name='menu')
  218.         self.motivation = Motivation(name='motivation')
  219.         self.achievements = Achievements(name='achievements')
  220.         self.stats = Stats(name='stats')
  221.         self.advice = Advice(name='advice')
  222.         self.screenmanager.add_widget(self.menu)
  223.         self.screenmanager.add_widget(self.motivation)
  224.         self.screenmanager.add_widget(self.achievements)
  225.         self.screenmanager.add_widget(self.stats)
  226.         self.screenmanager.add_widget(self.advice)
  227.         del screens[:]
  228.         screens.append(self.menu)
  229.         screens.append(self.motivation)
  230.         screens.append(self.achievements)
  231.         screens.append(self.stats)
  232.         screens.append(self.advice)
  233.         self.screenmanager.transition.direction = 'left'
  234.         print(screens)
  235.         return self.screenmanager
  236.  
  237. screens = []
  238. app = main()
  239. if __name__ in ('__main__', '__android__'):
  240.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement