我使用kivy.factory.Factory
打开弹出窗口,但当我想关闭它们时,它就不起作用了。在
代码:
from kivy.app import App
from kivy.lang import Builder
x = Builder.load_string("""
#:import F kivy.factory.Factory
#:import Window kivy.core.window.Window
Screen:
Button:
text: 'Press to Open First Popup'
on_press:
F.FirstPopup().open()
<FirstPopup@Popup>:
title: 'First Popup'
size_hint: None, None
width: Window.width / 1.4
height: Window.width / 1.4
Button:
text: 'Press to Open Second Popup'
on_press: F.SecondPopup().open()
<SecondPopup@Popup>:
title: 'Second Popup'
size_hint: None, None
width: Window.width / 1.8
height: Window.width / 1.8
Button:
text: 'Press to Close Both Popups'
on_press:
root.dismiss()
F.FirstPopup().dismiss() # < DOSEN'T WORK
""")
class MyApp(App):
def build(self):
return x
MyApp().run()
问题是,每次调用}second popup不同,换句话说,您关闭的是刚刚创建的弹出窗口而不是start。为了使其显而易见,您可以将代码更改为:
F.Foo()
时,都是在创建Foo类的一个新对象,因此在您的例子中,屏幕的F.FirstPopup().open()
与{获得以下信息:
^{pr2}$当你每次按下它时,你会得到一个新的id,表明它是一个新的对象。在
因此,一个可能的解决方案是保存由属性创建的对象的引用:
相关问题 更多 >
编程相关推荐