Advertisement
soulseb

poisk2

Jun 10th, 2025
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. from kivy.config import Config
  2. Config.set('graphics', 'width', '360')
  3. Config.set('graphics', 'height', '640')
  4. Config.set('graphics', 'resizable', '0')
  5.  
  6. from kivy.app import App
  7. from kivy.lang import Builder
  8. from kivy.animation import Animation
  9. from kivy.core.window import Window
  10. from kivy.metrics import dp
  11. from kivy.uix.boxlayout import BoxLayout
  12. from kivy.uix.button import Button
  13. from kivy.utils import platform
  14.  
  15. Builder.load_string('''
  16. <SearchAppUI>:
  17.    orientation: 'vertical'
  18.    spacing: dp(5)
  19.    
  20.    # Плашка результатов
  21.    BoxLayout:
  22.        id: result_panel
  23.        size_hint_y: None
  24.        height: 0
  25.        opacity: 0
  26.        canvas.before:
  27.            Color:
  28.                rgba: 0.2, 0.8, 0.4, 0.9
  29.            Rectangle:
  30.                pos: self.pos
  31.                size: self.size
  32.        Label:
  33.            id: result_label
  34.            text: 'Результат поиска'
  35.            color: 1, 1, 1, 1
  36.            font_size: dp(18)
  37.            bold: True
  38.            halign: 'center'
  39.    
  40.    # Основная область контента с кнопками
  41.    ScrollView:
  42.        id: main_scroll
  43.        size_hint_y: 1
  44.        do_scroll_y: not search_panel.is_raised
  45.        
  46.        BoxLayout:
  47.            id: content_box
  48.            orientation: 'vertical'
  49.            size_hint_y: None
  50.            height: self.minimum_height
  51.            spacing: dp(5)
  52.            padding: dp(10)
  53.    
  54.    # Поисковая панель
  55.    BoxLayout:
  56.        id: search_panel
  57.        size_hint_y: None
  58.        height: dp(60)
  59.        pos_hint: {'x': 0, 'y': 0}
  60.        padding: dp(10)
  61.        spacing: dp(5)
  62.        is_raised: False
  63.        
  64.        TextInput:
  65.            id: search_input
  66.            hint_text: 'Введите запрос...'
  67.            multiline: False
  68.            size_hint_x: 0.8
  69.            padding: [dp(10), (self.height - self.line_height)/2]
  70.            font_size: dp(16)
  71.            on_focus: root.on_search_focus(*args)
  72.        
  73.        Button:
  74.            text: 'Найти'
  75.            size_hint_x: 0.2
  76.            font_size: dp(16)
  77.            on_press: root.do_search()
  78. ''')
  79.  
  80. class SearchAppUI(BoxLayout):
  81.     def __init__(self, **kwargs):
  82.         super().__init__(**kwargs)
  83.         Window.bind(on_keyboard=self.on_keyboard)
  84.         self.keyboard_height = dp(300)
  85.         self.create_content_buttons()
  86.        
  87.         # Для Android добавляем обработчик кнопки назад
  88.         if platform == 'android':
  89.             from android.runnable import run_on_ui_thread
  90.             from jnius import autoclass
  91.             self._activity = autoclass('org.kivy.android.PythonActivity').mActivity
  92.             Window.bind(on_keyboard=self.on_keyboard)
  93.    
  94.     def on_keyboard(self, window, key, *args):
  95.         if key == 27:  # ESC или кнопка назад на Android
  96.             # Если поисковая панель поднята - сначала опускаем её
  97.             if self.ids.search_panel.is_raised:
  98.                 self.ids.search_input.focus = False
  99.                 return True
  100.             # Иначе выходим из приложения
  101.             App.get_running_app().stop()
  102.             return True
  103.    
  104.     def create_content_buttons(self):
  105.         """Создаем кнопки контента"""
  106.         content_box = self.ids.content_box
  107.         for i in range(1, 21):
  108.             btn = Button(
  109.                 text=f'Кнопка {i}',
  110.                 size_hint_y=None,
  111.                 height=dp(50),
  112.                 background_normal='',
  113.                 background_color=(0.4, 0.4, 0.6, 1),
  114.                 on_press=self.on_button_press
  115.             )
  116.             content_box.add_widget(btn)
  117.    
  118.     def on_button_press(self, instance):
  119.         self.ids.result_label.text = f"Выбрано: {instance.text}"
  120.         if self.ids.result_panel.height == 0:
  121.             Animation(height=dp(50), opacity=1, duration=0.3).start(self.ids.result_panel)
  122.    
  123.     def on_search_focus(self, instance, focused):
  124.         if focused:
  125.             self.ids.search_panel.is_raised = True
  126.             Animation(
  127.                 y=self.keyboard_height + dp(10),
  128.                 duration=0.2,
  129.                 t='out_quad'
  130.             ).start(self.ids.search_panel)
  131.             Animation(
  132.                 scroll_y=0,
  133.                 duration=0.3
  134.             ).start(self.ids.main_scroll)
  135.         else:
  136.             self.ids.search_panel.is_raised = False
  137.             Animation(
  138.                 y=0,
  139.                 duration=0.2,
  140.                 t='out_quad'
  141.             ).start(self.ids.search_panel)
  142.    
  143.     def do_search(self):
  144.         query = self.ids.search_input.text
  145.         if not query:
  146.             return
  147.            
  148.         self.ids.result_label.text = f"Найдено: {query}"
  149.         if self.ids.result_panel.height == 0:
  150.             Animation(height=dp(50), opacity=1, duration=0.3).start(self.ids.result_panel)
  151.         self.ids.search_input.focus = False
  152.  
  153. class SearchApp(App):
  154.     def build(self):
  155.         Window.clearcolor = (0.4, 0.4, 0.4, 1)
  156.         return SearchAppUI()
  157.  
  158. if __name__ == '__main__':
  159.     SearchApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement