Advertisement
shh_algo_PY

My First Kivy App

Mar 4th, 2023
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. # не забудь импортировать необходимые элементы!
  2. # Translation:
  3. # Don't forget to import the necessary elements!
  4.  
  5. from kivy.app import App
  6. from kivy.uix.label import Label
  7. from kivy.uix.button import Button
  8. from kivy.uix.textinput import TextInput
  9. from kivy.uix.boxlayout import BoxLayout
  10. from kivy.uix.screenmanager import ScreenManager, Screen
  11. from kivy.uix.scrollview import ScrollView
  12.  
  13. class ScrButton(Button):
  14.     def __init__(self, screen, direction='right', goal='main', **kwargs):
  15.         super().__init__(**kwargs)
  16.         self.screen = screen
  17.         self.direction = direction
  18.         self.goal = goal
  19.     def on_press(self):
  20.         self.screen.manager.transition.direction = self.direction
  21.         self.screen.manager.current = self.goal
  22.        
  23. class MainScr(Screen):
  24.     def __init__(self, **kwargs):
  25.         super().__init__(**kwargs)
  26.         vl = BoxLayout(orientation='vertical', padding=8, spacing=8)
  27.         hl = BoxLayout()
  28.         txt = Label(text= 'Choose a screen')
  29.         vl.add_widget(ScrButton(self, direction='down', goal='first', text="1"))
  30.         vl.add_widget(ScrButton(self, direction='left', goal='second', text="2"))
  31.         vl.add_widget(ScrButton(self, direction='up', goal='third', text="3"))
  32.         vl.add_widget(ScrButton(self, direction='right', goal='fourth', text="4"))
  33.         hl.add_widget(txt)
  34.         hl.add_widget(vl)
  35.         self.add_widget(hl)
  36.  
  37. class FirstScr(Screen):
  38.     def __init__(self, **kwargs):
  39.         super().__init__(**kwargs)
  40.         vl = BoxLayout(orientation='vertical', size_hint=(.5, .5), pos_hint={'center_x': 0.5, 'center_y': 0.5})
  41.         btn = Button(text= 'Choice: 1', size_hint=(.5, 1), pos_hint={'left': 0})
  42.         btn_back = ScrButton(self, direction='up', goal='main', text="Back", size_hint=(.5, 1), pos_hint={'right': 1})
  43.         vl.add_widget(btn)
  44.         vl.add_widget(btn_back)
  45.         self.add_widget(vl)
  46.  
  47. # WE STOPPED HERE (26th February 2023)
  48. class SecondScr(Screen):
  49.     def __init__(self, **kwargs):
  50.         super().__init__(**kwargs)
  51.         vl = BoxLayout(orientation='vertical')
  52.         self.txt = Label(text= 'Choice: 2')
  53.         vl.add_widget(self.txt)
  54.  
  55.         hl_0 = BoxLayout(size_hint=(0.8, None), height='30sp')
  56.         lbl1 = Label(text='Enter password:', halign='right')
  57.         self.input = TextInput(multiline=False)
  58.  
  59.         hl_0.add_widget(lbl1)
  60.         hl_0.add_widget(self.input)
  61.         vl.add_widget(hl_0)
  62.        
  63.         hl = BoxLayout(size_hint=(0.5, 0.2), pos_hint={'center_x': 0.5})
  64.         btn_false = Button(text="OK!")
  65.         btn_back = ScrButton(self, direction='right', goal='main', text="Back")
  66.  
  67.         hl.add_widget(btn_false)
  68.         hl.add_widget(btn_back)
  69.         vl.add_widget(hl)
  70.         self.add_widget(vl)
  71.         btn_false.on_press = self.change_text
  72.  
  73.     def change_text(self):
  74.         self.txt.text = self.input.text + "? Didn't work ..."
  75.  
  76. # SCREEN THREE HAS A BUTTON AND A LABEL
  77. class ThirdScr(Screen):
  78.     def __init__(self, **kwargs):
  79.         super().__init__(**kwargs)
  80.         layout = BoxLayout(orientation='vertical')
  81.         btn_back = ScrButton(self, direction='down', goal='main', text="Back", size_hint=(1, None), height='40sp')
  82.         test_label = Label(text = "Your own screen")
  83.         layout.add_widget(test_label)
  84.         layout.add_widget(btn_back)
  85.         self.add_widget(layout)
  86.  
  87. # SCREEN FOUR HAS SCROLLING ENABLED!
  88. class FourthScr(Screen):
  89.     def __init__(self, **kwargs):
  90.         super().__init__(**kwargs)
  91.         vl = BoxLayout(orientation='vertical', spacing=8)
  92.         a = 'START ' + 'Choice: 3 ' * 200
  93.         test_label = Label(text = "Extra exercise",size_hint=(0.3,None))
  94.  
  95.         btn_back = ScrButton(self, direction='left', goal='main', text="Back", size_hint=(1, .2), pos_hint={'center-x': 0.5})
  96.         self.label = Label(text=a, size_hint_y=None, font_size='24sp', halign='left', valign='top')
  97.         self.label.bind(size=self.resize)
  98.        
  99.         self.scroll = ScrollView(size_hint=(1, 1))
  100.         self.scroll.add_widget(self.label)
  101.  
  102.         vl.add_widget(test_label)
  103.         vl.add_widget(btn_back)
  104.         vl.add_widget(self.scroll)
  105.         self.add_widget(vl)
  106.  
  107.     def resize(self, *args):
  108.         self.label.text_size = (self.label.width, None)
  109.         self.label.texture_update()
  110.         self.label.height = self.label.texture_size[1]    
  111.  
  112. class MyApp(App):
  113.     def build(self):
  114.         sm = ScreenManager()
  115.         sm.add_widget(MainScr(name='main'))
  116.         sm.add_widget(FirstScr(name='first'))
  117.         sm.add_widget(SecondScr(name='second'))
  118.         sm.add_widget(ThirdScr(name='third'))
  119.         sm.add_widget(FourthScr(name='fourth'))
  120.         return sm
  121.  
  122. MyApp().run()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement