Advertisement
soulseb

poisk3

Jun 10th, 2025
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.55 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(16)
  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.            on_text_validate: root.do_search()
  73.        
  74.        Button:
  75.            id: action_button
  76.            text: 'Добавить'
  77.            size_hint_x: 0.2
  78.            font_size: dp(13)
  79.            bold: True
  80.            background_normal: ''
  81.            background_color: (0.4, 0.6, 0.4, 1)
  82.            on_press: root.toggle_result_panel()
  83. ''')
  84.  
  85. class SearchAppUI(BoxLayout):
  86.     def __init__(self, **kwargs):
  87.         super().__init__(**kwargs)
  88.         Window.bind(on_keyboard=self.on_keyboard)
  89.         self.keyboard_height = dp(300)
  90.         self.create_content_buttons()
  91.        
  92.         # Для Android добавляем обработчик кнопки назад
  93.         if platform == 'android':
  94.             from android.runnable import run_on_ui_thread
  95.             from jnius import autoclass
  96.             self._activity = autoclass('org.kivy.android.PythonActivity').mActivity
  97.             Window.bind(on_keyboard=self.on_keyboard)
  98.    
  99.     def on_keyboard(self, window, key, *args):
  100.         if key == 27:  # ESC или кнопка назад на Android
  101.             # Если поисковая панель поднята - сначала опускаем её
  102.             if self.ids.search_panel.is_raised:
  103.                 self.ids.search_input.focus = False
  104.                 return True
  105.             # Иначе выходим из приложения
  106.             App.get_running_app().stop()
  107.             return True
  108.    
  109.     def create_content_buttons(self):
  110.         """Создаем кнопки контента"""
  111.         content_box = self.ids.content_box
  112.         for i in range(1, 21):
  113.             btn = Button(
  114.                 text=f'Кнопка {i}',
  115.                 size_hint_y=None,
  116.                 height=dp(50),
  117.                 background_normal='',
  118.                 background_color=(0.4, 0.4, 0.6, 1),
  119.                 on_press=self.on_button_press
  120.             )
  121.             content_box.add_widget(btn)
  122.    
  123.     def on_button_press(self, instance):
  124.         self.ids.result_label.text = f"Выбрано: {instance.text}"
  125.         if self.ids.result_panel.height == 0:
  126.             self.toggle_result_panel(show=True)
  127.    
  128.     def on_search_focus(self, instance, focused):
  129.         if focused:
  130.             self.ids.search_panel.is_raised = True
  131.             Animation(
  132.                 y=self.keyboard_height + dp(10),
  133.                 duration=0.2,
  134.                 t='out_quad'
  135.             ).start(self.ids.search_panel)
  136.             Animation(
  137.                 scroll_y=0,
  138.                 duration=0.3
  139.             ).start(self.ids.main_scroll)
  140.         else:
  141.             self.ids.search_panel.is_raised = False
  142.             Animation(
  143.                 y=0,
  144.                 duration=0.2,
  145.                 t='out_quad'
  146.             ).start(self.ids.search_panel)
  147.    
  148.     def toggle_result_panel(self, show=None):
  149.         """Переключает видимость панели результатов"""
  150.         result_panel = self.ids.result_panel
  151.         action_button = self.ids.action_button
  152.         search_input = self.ids.search_input
  153.        
  154.         if show is None:
  155.             show = result_panel.height == 0
  156.        
  157.         if show:
  158.             Animation(height=dp(35), opacity=1, duration=0.3).start(result_panel)
  159.             action_button.text = "Убрать"
  160.             action_button.background_color = (0.8, 0.3, 0.3, 1)  # Красный цвет для "Убрать"
  161.         else:
  162.             Animation(height=0, opacity=0, duration=0.3).start(result_panel)
  163.             action_button.text = "Добавить"
  164.             action_button.background_color = (0.4, 0.6, 0.4, 1)  # Зеленый цвет для "Добавить"
  165.             # Закрываем клавиатуру только если поле ввода не в фокусе
  166.             if not search_input.focus:
  167.                 search_input.focus = False
  168.    
  169.     def do_search(self):
  170.         """Выполняет поиск по введенному тексту"""
  171.         query = self.ids.search_input.text.strip()
  172.         if query:
  173.             self.ids.result_label.text = f"Найдено: {query}"
  174.             if self.ids.result_panel.height == 0:
  175.                 self.toggle_result_panel(show=True)
  176.         # Не снимаем фокус с поля ввода после поиска
  177.         # self.ids.search_input.focus = False
  178.  
  179. class SearchApp(App):
  180.     def build(self):
  181.         Window.clearcolor = (0.4, 0.4, 0.4, 1)
  182.         return SearchAppUI()
  183.  
  184. if __name__ == '__main__':
  185.     SearchApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement