Popup auto dismissal

Asked by At

Environment:

I have a main screen and two popups. When the button from main screen is clicked first popup will comeup. Selecting the options in the first popup and pressing the button of that popup should dismiss first popup and open the second popup. Upto here is acheived.

multiple_popup.py

import kivy
kivy.require('1.11.0')
import os
os.environ['KIVY_GL_BACKEND'] = 'gl'
import time
from kivy.app import App
from kivy.core.text import LabelBase
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock

class MainScreen(BoxLayout):

    sw_switch = 'none'  

    def process_1(self):

        print("The process will be running here")
        time.sleep(5)
        self.sw_switch = 'over' 


    def setName(self,*args):
        FirstPopup().open() 


    def setName1(self,*args):
        self.sw_switch = 'true1' 
        SecondPopup().open()    

#------------------------------------
class FirstPopup(Popup):

    def popup1_but(self):
        self.dismiss()

class SecondPopup(Popup):

    def popup2_but(self):
        self.dismiss()
    pass
#------------------------------------

class MultPopApp(App):
    def build(self):
        Clock.schedule_interval(lambda dt: self.update_time(), 1)
        return MainScreen()

    def update_time(self):
        if self.root.sw_switch == 'true':
            self.root.process_1()

        if self.root.sw_switch == 'over':
            x = SecondPopup()
            x.popup2_but()
            self.root.sw_switch = 'none'


if __name__ == '__main__':
    MultPopApp().run()      

multpop.kv

#: kivy 1.11.0

<MainScreen>:
    orientation: 'vertical'
    Button:
        text: 'Options'
        on_release: root.setName(*args)

<FirstPopup>:
    title: 'Options Window'
    size_hint: None, None
    size: 400,370
    BoxLayout:
        orientation : 'vertical'
        Label:  
            text : "Checkbox options listed here"
        Button:
            text: "OK"
            on_press: 
                app.root.setName1(*args)
                app.root.sw_switch = 'true'
                root.popup1_but()


<SecondPopup>:
    title: 'Please wait..'
    size_hint: None, None
    size: 400,370
    BoxLayout:
        orientation : 'vertical'
        size_hint: 1.0,1.0

        Label:  
            text : "Process Initiated"
            size_hint: 1.0, 1.0

Now I need this second popup to auto dismiss after a background function initiated alongside this second pop. The solution in this code is which I have arrived after trying few other stuffs. Any guidance is highly appreciated. Thank you.

Both python and kivy files are posted

1 Answers

0
ikolim On Best Solutions

First Popup - auto_dismiss: False

By default, auto_dismiss is set to True i.e. any click outside the popup will dismiss/close it. If you don’t want that, you can set auto_dismiss to False.

Snippets - kv

<FirstPopup>:
    title: 'Options Window'
    auto_dismiss: False

Second Popup - auto_dismiss: True

Now I need this second popup to auto dismiss after a background function initiated alongside this second pop.

Solution

In order to be able to access the second Popup widget, do the following

  • Add an import statement, from kivy.properties import ObjectProperty
  • Declare a class attribute, popup2 of type ObjectProperty and initialized to None
  • Assign the instantiation of SecondPopup() to self.popup2
  • Open popup using self.popup2.open()
  • You can auto dismiss the SecondPopup() using self.popup2.dismiss(). Use this either when the background function is initiated, completed or use Clock to schedule a callback.

Snippets - py

from kivy.properties import ObjectProperty


class MainScreen(BoxLayout):
    popup2 = ObjectProperty(None)

    ...

    def setName1(self, *args):
        self.sw_switch = 'true1'
        self.popup2 = SecondPopup()
        self.popup2.open()
        Clock.schedule_once(lambda dt: self.dismiss_popup2(), 5)

    def dismiss_popup2(self):
        print(f"\ndismiss_popup2: SecondPopup dismissed")
        self.popup2.dismiss()

Note » Kivy App - Avoid sleep() or infinite loops

In Kivy applications, you have to avoid long/infinite loops or sleeping.

while True:
    animate_something()
    time.sleep(.10)

When you run this, the program will never exit your loop, preventing Kivy from doing all of the other things that need doing. As a result, all you’ll see is a black window which you won’t be able to interact with. Instead, you need to “schedule” your animate_something() function to be called repeatedly.