Reputation: 77
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.
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()
#: 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
Upvotes: 1
Views: 939
Reputation: 16041
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
.
<FirstPopup>:
title: 'Options Window'
auto_dismiss: False
auto_dismiss: True
Now I need this second popup to auto dismiss after a background function initiated alongside this second pop.
In order to be able to access the second Popup widget, do the following
from kivy.properties import ObjectProperty
popup2
of type ObjectProperty
and initialized to None
SecondPopup()
to self.popup2
self.popup2.open()
SecondPopup()
using self.popup2.dismiss()
. Use this either when the background function is initiated, completed or use Clock to schedule a callback.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()
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.
Upvotes: 1