Kivy: Dismiss One Popup From Another Popup
I use kivy.factory.Factory to open the popups, but it's not working when I want to close them. Code: from kivy.app import App from kivy.lang import Builder x = Builder.load_strin
Solution 1:
The problem is that every time you call F.Foo()
you are creating a new object of the Foo class, so in your case F.FirstPopup().open()
of the Screen is different from F.FirstPopup().dismiss()
SecondPopup, in other words you are closing a popup that you have just created instead of the start. To make it obvious, you can change your code to:
# ...
Button:
text: 'Press to Close Both Popups'
on_press:
print(F.FirstPopup())
Obtaining the following:
<kivy.factory.FirstPopup object at 0x7f8f9a183e18>
<kivy.factory.FirstPopup object at 0x7f8f996fc118>
<kivy.factory.FirstPopup object at 0x7f8f996fc388>
<kivy.factory.FirstPopup object at 0x7f8f996fc5f8>
<kivy.factory.FirstPopup object at 0x7f8f996fc528>
<kivy.factory.FirstPopup object at 0x7f8f996fc2b8>
<kivy.factory.FirstPopup object at 0x7f8f996fc048>
And as you see each time you press it you get a new id indicating that it is a new object.
So a possible solution is to save a reference of the object created by a property:
fromkivy.appimportAppfromkivy.langimportBuilderx=Builder.load_string("""#:import F kivy.factory.Factory#:import Window kivy.core.window.WindowScreen:Button:text:'Press to Open First Popup'on_press:F.FirstPopup().open()<FirstPopup@Popup>:title:'First Popup'size_hint:None,Nonewidth:Window.width/1.4height:Window.width/1.4Button:text:'Press to Open Second Popup'on_press:second_popup=F.SecondPopup()second_popup.first_popup=rootsecond_popup.open()<SecondPopup@Popup>:title:'Second Popup'size_hint:None,Nonewidth:Window.width/1.8height:Window.width/1.8first_popup:NoneButton:text:'Press to Close Both Popups'on_press:root.dismiss()if root.first_popup is not None:root.first_popup.dismiss()""")
class MyApp(App):
def build(self):
return x
MyApp().run()
Post a Comment for "Kivy: Dismiss One Popup From Another Popup"