Search code examples
pythonpython-3.xuser-interfacekivykivy-language

Kivy, make sometihing happen 1 sec after i press a button


I have tried for many hours, reading the wiki and experimenting in different ways but I have not been able to do it, can someone please explain to me explicitly (showing me) how to do this without only linking me to the wiki. Below you will find my code copied, where you read on_release: under the button with its id "settings", I want it to actually happen not on release but 1 sec after I have pressed the button.

.py file:

import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout

#title
#self.title('Past Paper Question Search 🔎')
#background
Window.clearcolor = (0.145, 0.145, 0.149, 1)
#resizability
Config.set('graphics', 'resizable', True)
class FloatLayout(FloatLayout):
    def __init__(self, **kwargs):
        super(FloatLayout, self).__init__(**kwargs)


class GUIApp(App):
    def build(self):
        return FloatLayout()

if __name__ == "__main__":
    GUIApp().run()

.kv file:

<FloatLayout>:
    BoxLayout:
        id: searchBox
        orientation: 'horizontal'
        size_hint_x: 0.6
        size_hint_y: 0.07
        pos_hint: {"x":0.2, "y":0.7}
        TextInput:
            background_color: 0.275, 0.275, 0.275, 1
            multiline: False
            font_size: self.height/1.6
            foreground_color: [1, 1, 1, 1]
            hint_text: "Search"
            hint_text_color: [1, 1, 1, 0.3]
            cursor_color: [1, 1, 1, 1]
            padding: [(self.height)/2-(self.line_height/2.0), (self.height)/2-(self.line_height/2.0), 6, 0]
        Button:
            size_hint_y: 1
            width: self.height
            size_hint_x: None
            border: 0, 0, 0, 0
            background_normal: "resources/Search.png"
            background_down: "resources/Search_pressed.png"  
    Button:
        id: settings
        size_hint_y: 0.095
        width: self.height
        size_hint_x: None
        border: 0, 0, 0, 0
        background_normal: "resources/empty.png"
        background_down: "resources/empty.png"
        pos_hint: {"x":0.92, "y":0.9}
        on_press: gif.anim_delay = 0.10
        on_press: gif._coreimage.anim_reset(True)
        on_release: settings.pos_hint= {"x":0.2, "y":2}
        on_release: close.pos_hint= {"x":0.92, "y":0.9}
        on_release: searchBox.pos_hint= {"x":0.2, "y":2.7}
        Image:
            id: gif
            source: 'resources/settings_gif.gif'
            center: self.parent.center
            size: self.parent.size
            anim_delay: -1
            anim_loop: 1
            allow_stretch: True
    
    
    Button:
        id: close
        size_hint_y: 0.095
        width: self.height
        size_hint_x: None
        border: 0, 0, 0, 0
        background_normal: "resources/X.png"
        background_down: "resources/X.png"
        on_press: searchBox.pos_hint= {"x":0.2, "y":0.7}
        on_press: settings.pos_hint= {"x":0.92, "y":0.9}
        on_press: close.pos_hint= {"x":0.92, "y":2.9}
        pos_hint: {"x":0.92, "y":2.9}
            

Thx in advance


Solution

  • Here is a small example. Read about Clock: https://kivy.org/doc/master/api-kivy.clock.html?highlight=clock#module-kivy.clock

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.clock import Clock
    
    kv = """
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: status
        Button:
            size_hint_y: None
            height: 48
            text: 'Press'
            on_release: app.schedule_action()
    """
    
    
    class WaitOneSecondApp(App):
        def build(self):
            return Builder.load_string(kv)
    
        def schedule_action(self):
            self.root.ids.status.text = 'Waiting One Second...'
            Clock.schedule_once(self.wait_over, 1)
    
        def wait_over(self, dt):
            self.root.ids.status.text = 'The wait is over'
    
    
    WaitOneSecondApp().run()